1.第一步,添加爆炸动画
~~~
//添加玩家飞机飞行动画
id _playerFlyAction;
id _playerBlowupAnimation; //战机爆炸动画
id _enemyBlowupAnimation;//敌机爆炸动画
BOOL _isEnemyCollodable; //敌机是否可碰撞
BOOL _isPlayerCollodable;//玩家飞机是否可碰撞
~~~
2.制作精灵表单
![](https://box.kancloud.cn/2016-08-22_57bab559080c9.jpg)
![](https://box.kancloud.cn/2016-08-22_57bab5592bd57.jpg)
3.初始化爆炸量
~~~
//初始化爆炸效果的量
_playerBlowupAnimation = [self getAnimationByName:@"plane_bao_" delay:0.08 animNum:5];
[_playerBlowupAnimation retain];
_enemyBlowupAnimation = [self getAnimationByName:@"plane2_bao_" delay:0.08 animNum:5];
[_enemyBlowupAnimation retain];
_isEnemyCollodable = YES;
_isPlayerCollodable = YES;
~~~
4.添加获取动画帧的图片 的方法
~~~
#pragma mark 飞机飞行和爆炸动画
- (CCAnimation *)getAnimationByName:(NSString *)animName delay:(float)delay animNum:(int)num
{
NSMutableArray *animFrames = [NSMutableArray arrayWithCapacity:num];
for (int i=1; i<= num; ++i) {
NSString *frameName = [NSString stringWithFormat:@"%@%d.png",animName,i];
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animFrames delay:delay];
return animation;
}
~~~
5.修改碰撞
~~~
-(void) collisionDetection:(ccTime)dt{
CCSprite *enemy;
// CGRect bulletRect = [self rectOfSprite:_bulletSprite];
CCARRAY_FOREACH(_enemySprites, enemy)
{
if (enemy.visible && _isEnemyCollodable ) {
_isEnemyCollodable = NO;
//1.bullet & enemy collision det ection
// CGRect enemyRect = [self rectOfSprite:enemy];
if (_bulletSprite.visible && CGRectIntersectsRect(enemy.boundingBox, _bulletSprite.boundingBox)) {
//使用CCspawn 动作组合
id ac1 = [CCScaleTo actionWithDuration:1.0 scale:1.2];
id ac2 = [CCRotateBy actionWithDuration:1.0 angle:720];
id ac3 = [CCFadeOut actionWithDuration:1.0];
id ac4 = [CCHide action];
id blowup = [CCAnimate actionWithAnimation:_enemyBlowupAnimation ];
id block = ^(){
_isEnemyCollodable = YES;
};
id ac5 = [CCSequence actions:ac3,ac4,[CCCallBlock actionWithBlock:block], nil];
id action = [CCSpawn actions:ac1,ac2,ac5,blowup, nil];
[enemy stopAllActions];
[enemy runAction:action];
enemy.visible = NO;
_bulletSprite.visible = NO;
_totalScore += 100;
if (_totalScore >= 1000) {
[_gameEndLabel setString:@"游戏胜利!"];
_gameEndLabel.visible = YES;
id scaleTo = [CCScaleTo actionWithDuration:1.0 scale:1.2f];
[_gameEndLabel runAction:scaleTo];
[self unscheduleUpdate];
[self performSelector:@selector(onRestartGame) withObject:nil afterDelay:2.0f];
}
[_bulletSprite stopAllActions];
[enemy stopAllActions];
CCLOG(@"collision bullet");
break;
}
//2.enemy & player collision detection
CCSprite *playerSprite = [self getPlayerSprite];
// CGRect playRect = [self rectOfSprite:playerSprite];
if (playerSprite.visible && _isPlayerCollodable &&
playerSprite.numberOfRunningActions == 0
&& CGRectIntersectsRect(enemy.boundingBox, playerSprite.boundingBox)) {
enemy.visible = NO;
_isPlayerCollodable = NO;
_totalLives -= 1;
if (_totalLives <= 0) {
[_gameEndLabel setString:@"游戏失败!"];
_gameEndLabel.visible = YES;
id scaleTo = [CCScaleTo actionWithDuration:1.0 scale:1.2f];
[_gameEndLabel runAction:scaleTo];
[self unscheduleUpdate];
[self performSelector:@selector(onRestartGame) withObject:nil afterDelay:3.0f];
}
id blink = [CCBlink actionWithDuration:2.0 blinks:4];
id blowup = [CCAnimate actionWithAnimation:_playerBlowupAnimation];
id action = [CCSequence actions:blowup,blink,[CCCallBlock actionWithBlock:^(){
_isPlayerCollodable = YES;[playerSprite stopAllActions];
[playerSprite runAction:_playerFlyAction];
playerSprite.opacity = 255;
playerSprite.visible = YES;}], nil];
[playerSprite stopAllActions];
[playerSprite runAction:action];
CCLOG(@"collision player");
break;
}
}
}
}
~~~