下载地址:http://download.csdn.net/detail/qq348931837/6372943 欢迎下载!!!
需要打飞机素材的可点击 点击打开链接下载完整素材!!!链接素材是一张张的图片,因为考虑到不需要使用纹理的同学所有重新把纹理图分开!!!
基本功能已经实现,空降物品也实现了(炸弹和子弹)!!!!
基本代码如下:
下落物品类
DropBombAndBullet.h #import <Foundation/Foundation.h> #import "cocos2d.h" typedef enum{ dropBomb=4, dropBullet=5 }dropType; @interface DropBombAndBullet : CCNode { CCSprite *_drop; //下落的精灵 dropType _bombAndBullet; //类型:子弹或者炸弹 } @property(assign)CCSprite *drop; @property(assign)dropType bombAndBullet; - (void) initWithType:(dropType)type; - (void) dropAnimation; @end
DropBombAndBullet.m #import "DropBombAndBullet.h" @implementation DropBombAndBullet @synthesize drop=_drop; @synthesize bombAndBullet=_bombAndBullet; -(void)initWithType:(dropType)type{ self.bombAndBullet=type; NSString *proKye=[NSString stringWithFormat:@"enemy%d_fly_1.png",type]; self.drop=[CCSprite spriteWithSpriteFrameName:proKye]; //arc4random()%268 获取0到267之间的整数 arc4random()产生随机数是rand()2倍 (4294967296) [self.drop setPosition:ccp(arc4random()%268, 732)]; } // <---------------------------下落物品的动画-------------------------------> -(void)dropAnimation { id act1=[CCMoveTo actionWithDuration:1 position:ccp(self.drop.position.x, 250)]; //移到400px的位置 id act2=[CCMoveTo actionWithDuration:0.4 position:ccp(self.drop.position.x, 252)];//停留以下 id act3=[CCMoveTo actionWithDuration:1 position:ccp(self.drop.position.x, 732)]; //再上升 id act4=[CCMoveTo actionWithDuration:2 position:ccp(self.drop.position.x, -55)]; //下落 [self.drop runAction:[CCSequence actions:act1,act2,act3,act4, nil]]; } @end
敌人飞机类
FoePlane.h #import <Foundation/Foundation.h> #import "cocos2d.h" //敌人飞机 @interface FoePlane : CCSprite { int _planeType; //飞机类型 小飞机 中等飞机 打飞机 int _hp; //飞机的血 挨多少子弹砸毁 int _speed; //飞机的速度 } @property (readwrite) int planeType; @property (readwrite) int hp; @property (readwrite) int speed; @end
FoePlane.m #import "FoePlane.h" @implementation FoePlane @synthesize planeType=_planeType; @synthesize hp=_hp; @synthesize speed=_speed; @end
实现动画类 部分代码 如下:
一下为在实现动画类所要定义的方法 +(CCScene *) scene; //初始化数据 - (void) initData; //背景 - (void) loadBackground; - (void) scrollBackground; //玩家飞机 - (void) loadPlayer; //制造子弹 开火 移动 - (void) madeBullet; - (void) firingBullets; - (void) resetBullet; //制造敌人飞机 - (void) addFeoPlane; //制造小飞机 - (FoePlane *) makeSmallFoePlane; //制造中等飞机 - (FoePlane *) makeMediumFoePlane; //制造大飞机 - (FoePlane *) makeBigFoePlane; //飞机移动 - (void) moveFoePlane; //碰撞检测 - (void) collisionDetection; //添加空降物品 - (void) addBulletTypeTip; //设置游戏属性 - (void) gamePause; //暂停 - (void) gameStart; //开始 //子弹持续时间 - (void) bulletLastTime;
#pragma -mark 碰撞检测 - (void) foePlaneBlowupAnimation:(FoePlane *) foePlane { int animationNum = 0; if (foePlane.planeType == 1) { animationNum = 4; scoreInt += 2000; } if (foePlane.planeType == 3) { animationNum = 4; scoreInt += 10000; } if (foePlane.planeType == 2) { animationNum = 7; scoreInt += 40000; } [scoreLabel setString:[NSString stringWithFormat:@"%d",scoreInt]]; //首先飞机停止所有动画 [foePlane stopAllActions]; //动画集合--就是图片 NSMutableArray *foeActionArr = [NSMutableArray array]; for (int i = 1; i <= animationNum; i ++) { NSString *key = [NSString stringWithFormat:@"enemy%d_blowup_%i.png",foePlane.planeType,i]; //取出图片 CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:key]; //加入集合中 [foeActionArr addObject:frame]; } //定义运行动画的属性 CCAnimation *aniPlayer = [CCAnimation animationWithFrames:foeActionArr delay:0.1f]; id actFowPlane = [CCAnimate actionWithAnimation:aniPlayer]; id end = [CCCallFuncN actionWithTarget:self selector:@selector(blowupEnd:)]; //清空缓存的数组 [foeActionArr removeAllObjects]; //动画运行 [foePlane runAction:[CCSequence actions:actFowPlane,end, nil]]; if (foePlane.planeType == 3) { [self mediumPlaneDownSound]; } else if (foePlane.planeType == 2) { [self bigPlaneDownSound]; } else if (foePlane.planeType == 1) { [self smallPlaneDownSound]; } } - (void) blowupEnd:(id) sender { FoePlane *tmpPlane = (FoePlane *)sender; [tmpPlane removeFromParentAndCleanup:NO]; } - (void) collisionDetection { //获取当前子弹所在矩形框 CGRect bulletRect = bullet.boundingBox; for (FoePlane *tmpPlane in foePlanes) { if (CGRectIntersectsRect(bulletRect, tmpPlane.boundingBox)) { //子弹重设 [self resetBullet]; //判读敌人飞机是否还有血 tmpPlane.hp = tmpPlane.hp - (isBigBullet?2:1);//一个子弹一滴血 if (tmpPlane.hp <= 0) { //运行爆炸动画 [self foePlaneBlowupAnimation:tmpPlane]; [foePlanes removeObject:tmpPlane]; } else { //每个子弹都有打击效果 主要是中等飞机和大飞机 [self hitAnimationToFoePlane:tmpPlane]; } } } //检测敌人飞机是否撞到我 CGRect playerRec = player.boundingBox; playerRec.origin.x += 25; playerRec.size.width -= 50; playerRec.origin.y -= 10; playerRec.size.height -= 10; for (FoePlane *tmpPlane in foePlanes) { if (CGRectIntersectsRect(playerRec, tmpPlane.boundingBox)) { [self gameOver]; [self playerBlowupAnimation]; [self foePlaneBlowupAnimation:tmpPlane]; [foePlanes removeObject:tmpPlane]; } } //判断飞机是否吃到空降物品 if (isVisible == YES) { CGRect playerRect1 = player.boundingBox; CGRect propRect = prop.drop.boundingBox; if (CGRectIntersectsRect(playerRect1, propRect)) { [prop.drop stopAllActions]; [prop.drop removeFromParentAndCleanup:YES]; isVisible = NO; if (prop.bombAndBullet == dropBullet) { isBigBullet = YES; isChangeBullet = YES; } else { for (FoePlane *tmpPlane in foePlanes) { [self foePlaneBlowupAnimation:tmpPlane]; } [foePlanes removeAllObjects]; } } } }