zoukankan      html  css  js  c++  java
  • [一位菜鸟的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;
                }
            }
        }
    }
    



  • 相关阅读:
    谷歌开发调试工具
    由form表单来说说前后台数据之间的交互
    ajax的post和get请求的使用
    css各属性的理解
    Http Servlet详解及学习地址
    表单详细解释
    JS正则表达式
    jQuery-AJAX简介
    POJ1008 Maya Calendar
    关于Code Blocks无编译器版本及VC6.0插入、打开报错
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3481590.html
Copyright © 2011-2022 走看看