diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7597e94 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +# Xcode +.DS_Store +*/build/* +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +xcuserdata +profile +*.moved-aside +DerivedData +.idea/ +*.hmap + +#CocoaPods +Pods \ No newline at end of file diff --git a/PlaneWar.xcodeproj/project.pbxproj b/PlaneWar.xcodeproj/project.pbxproj index 78bdc2a..b079706 100644 --- a/PlaneWar.xcodeproj/project.pbxproj +++ b/PlaneWar.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + A40FF94D17C1F8AF00CA29BB /* Common.m in Sources */ = {isa = PBXBuildFile; fileRef = A40FF94C17C1F8AF00CA29BB /* Common.m */; }; A9466EDF17BDDC9E009232EA /* gameArts-hd.plist in Resources */ = {isa = PBXBuildFile; fileRef = A9466EDD17BDDC9E009232EA /* gameArts-hd.plist */; }; A9466EE017BDDC9E009232EA /* gameArts-hd.png in Resources */ = {isa = PBXBuildFile; fileRef = A9466EDE17BDDC9E009232EA /* gameArts-hd.png */; }; A948CDCF17BDD5A300BB9EC9 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A948CDCE17BDD5A300BB9EC9 /* QuartzCore.framework */; }; @@ -149,6 +150,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + A40FF94B17C1F8AF00CA29BB /* Common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Common.h; sourceTree = ""; }; + A40FF94C17C1F8AF00CA29BB /* Common.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Common.m; sourceTree = ""; }; A9466EDD17BDDC9E009232EA /* gameArts-hd.plist */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = "gameArts-hd.plist"; sourceTree = ""; }; A9466EDE17BDDC9E009232EA /* gameArts-hd.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "gameArts-hd.png"; sourceTree = ""; }; A948CDCB17BDD5A300BB9EC9 /* PlaneWar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PlaneWar.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -498,14 +501,16 @@ children = ( A948CF7317BDD5A500BB9EC9 /* AppDelegate.h */, A948CF7417BDD5A500BB9EC9 /* AppDelegate.m */, - A948CF7617BDD5A500BB9EC9 /* HelloWorldLayer.h */, - A948CF7717BDD5A500BB9EC9 /* HelloWorldLayer.m */, + A40FF94B17C1F8AF00CA29BB /* Common.h */, + A40FF94C17C1F8AF00CA29BB /* Common.m */, A948CF8117BDD5F200BB9EC9 /* GameLayer.h */, A948CF8217BDD5F200BB9EC9 /* GameLayer.m */, + A948CF7617BDD5A500BB9EC9 /* HelloWorldLayer.h */, + A948CF7717BDD5A500BB9EC9 /* HelloWorldLayer.m */, A948CF7917BDD5A500BB9EC9 /* IntroLayer.h */, A948CF7A17BDD5A500BB9EC9 /* IntroLayer.m */, - A948CDE317BDD5A300BB9EC9 /* Resources */, A948CDFB17BDD5A300BB9EC9 /* libs */, + A948CDE317BDD5A300BB9EC9 /* Resources */, A948CF6F17BDD5A500BB9EC9 /* Supporting Files */, ); path = PlaneWar; @@ -1086,6 +1091,7 @@ A948CF7817BDD5A500BB9EC9 /* HelloWorldLayer.m in Sources */, A948CF7B17BDD5A500BB9EC9 /* IntroLayer.m in Sources */, A948CF8317BDD5F200BB9EC9 /* GameLayer.m in Sources */, + A40FF94D17C1F8AF00CA29BB /* Common.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/PlaneWar/Common.h b/PlaneWar/Common.h new file mode 100644 index 0000000..fd48f21 --- /dev/null +++ b/PlaneWar/Common.h @@ -0,0 +1,13 @@ +// +// Common.h +// PlaneWar +// +// Created by jy on 13-8-19. +// Copyright (c) 2013年 GetToSet. All rights reserved. +// + +#import + +CGFloat randomFloatRange(CGFloat begin, CGFloat end); +NSInteger randomIntRange(NSInteger begin, NSInteger end); + diff --git a/PlaneWar/Common.m b/PlaneWar/Common.m new file mode 100644 index 0000000..17bfb01 --- /dev/null +++ b/PlaneWar/Common.m @@ -0,0 +1,19 @@ +// +// Common.m +// PlaneWar +// +// Created by jy on 13-8-19. +// Copyright (c) 2013年 GetToSet. All rights reserved. +// +#import "Common.h" + +#define ARC4RANDOM_MAX 0x100000000 + +CGFloat randomFloatRange(CGFloat begin, CGFloat end){ + CGFloat range = end - begin; + return ((CGFloat)arc4random() / ARC4RANDOM_MAX) * range + begin; +} + +NSInteger randomIntRange(NSInteger begin, NSInteger end){ + return arc4random()%(end-begin+1)+begin; +} diff --git a/PlaneWar/GameLayer.h b/PlaneWar/GameLayer.h index 4a1e8a6..a5aa1af 100644 --- a/PlaneWar/GameLayer.h +++ b/PlaneWar/GameLayer.h @@ -15,6 +15,8 @@ CCSprite *playerPlane; CCSprite *bomb; + + CCSprite *pause; CCLabelTTF *scoreLabel; diff --git a/PlaneWar/GameLayer.m b/PlaneWar/GameLayer.m index a7481b5..4273683 100644 --- a/PlaneWar/GameLayer.m +++ b/PlaneWar/GameLayer.m @@ -7,9 +7,20 @@ // #import "GameLayer.h" +#import "Common.h" @implementation GameLayer +/** + * 防止出现短时间内击中敌机使得其HP<0导致的type改变 + * 原理:tag = type * 100 + hp, + * 如果敌机type = 2 ,hp = 0,在系统未完成敌机死亡检测时再次被击中,则从tag计算得出的type变成1 + * 加上Buffer后,即使hp变成-1,tag值依然大于type * 100 + * 计算hp时减去Buffer即可 + * PS 依然是ugly hack + */ +static NSInteger hpBuffer = 10; + //基本方法 +(CCScene*)scene{ @@ -34,6 +45,7 @@ -(id)init{ [self startShowEnemies]; [self startCheckCollision]; [self loadBombButton]; + [self loadPauseButton]; [self startShowProps]; [self loadScoreLabel]; } @@ -168,11 +180,11 @@ -(void)showEnemy{ CCSprite *enemy=[CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"enemy%i_fly_1.png",type]]; enemy.anchorPoint=ccp(0.5,0); - enemy.position=ccp(arc4random()%(int)(winSize.width+1),winSize.height); + enemy.position=ccp(randomFloatRange(20.0, winSize.width-20.0),winSize.height); [self addChild:enemy z:4]; //Tag用于记录敌机类型和HP值(这比再写一个类要方便多了) - [enemy setTag:type*100+hp]; + [enemy setTag:type*100+hp+hpBuffer]; [enemies addObject:enemy]; @@ -181,7 +193,7 @@ -(void)showEnemy{ [enemy runAction:action]; } - id enemyMoveDown=[CCMoveBy actionWithDuration:5.0f position:ccp(0,-winSize.height-enemy.boundingBox.size.height)]; + id enemyMoveDown=[CCMoveBy actionWithDuration:(randomFloatRange(3.0, 6.0)) position:ccp(0,-winSize.height-enemy.boundingBox.size.height)]; id enemyMoveEnd=[CCCallFuncND actionWithTarget:self selector:@selector(enemyMoveEndedWithAction:Sprite:) data:enemy]; [enemy runAction:[CCSequence actions:enemyMoveDown,enemyMoveEnd,nil]]; @@ -395,6 +407,14 @@ -(void)loadBombButton{ [bomb setVisible:NO]; } +-(void)loadPauseButton{ + pause=[CCSprite spriteWithSpriteFrameName:@"game_pause.png"]; + pause.anchorPoint = ccp(0,0); + pause.position = ccp(winSize.width*0.85, winSize.height*0.9); + [self addChild:pause]; + [pause setVisible:YES]; +} + //触摸处理 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ @@ -415,11 +435,20 @@ -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [enemies removeAllObjects]; } + }else if (CGRectContainsPoint(pause.boundingBox, touchLocation)){ + if ([[CCDirector sharedDirector] isPaused]) { + [[CCDirector sharedDirector] resume]; + }else{ + [[CCDirector sharedDirector] pause]; + } } } -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ - + if ([[CCDirector sharedDirector] isPaused]) { + return; + } + UITouch *touch=[touches anyObject]; CGPoint touchLocation=[touch locationInView:touch.view]; touchLocation=[[CCDirector sharedDirector]convertToGL:touchLocation]; @@ -429,11 +458,9 @@ -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ CGPoint translation=ccpSub(touchLocation,oldTouchLocation); - if(CGRectContainsPoint(playerPlane.boundingBox,touchLocation)){ - CGPoint newPos = ccpAdd(playerPlane.position,translation); - if(CGRectContainsRect(CGRectMake(0,0,winSize.width,winSize.height),[self newRectWithSize:playerPlane.boundingBox.size Point:newPos AnchorPoint:ccp(0.5,0.5)])){ - playerPlane.position = newPos; - } + CGPoint newPos = ccpAdd(playerPlane.position,translation); + if(CGRectContainsRect(CGRectMake(0,0,winSize.width,winSize.height),[self newRectWithSize:playerPlane.boundingBox.size Point:newPos AnchorPoint:ccp(0.5,0.5)])){ + playerPlane.position = newPos; } } @@ -469,7 +496,7 @@ -(int)getEnemyTypeWithTag:(NSUInteger)tag{ } -(int)getEnemyHpWithTag:(NSUInteger)tag{ - return tag%100; + return tag%100-hpBuffer; } //对话框回调