[一位菜鸟的COCOS-2D编程之路]打飞机中机种敌机和战机损毁时的爆炸效果

1.第一步,添加爆炸动画

 

    //添加玩家飞机飞行动画
    id _playerFlyAction;
    
    id _playerBlowupAnimation; //战机爆炸动画
    id _enemyBlowupAnimation;//敌机爆炸动画
    BOOL _isEnemyCollodable;   //敌机是否可碰撞
    BOOL _isPlayerCollodable;//玩家飞机是否可碰撞


2.制作精灵表单

 


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;
            }
        }
    }
}


 


 

转载于:https://www.cnblogs.com/fuhaots2009/p/3481590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值