[一位菜鸟的COCOS-2D编程之路]COCOS2D中得动作,特效和动画

一,CCActionManager 管理所有节点动作的对象

来看看打飞机里面的一个onEnter 方法

 

- (void)onEnter

{

    [super onEnter]; //一定要注意添加此方法,否则将停留在开始界面

    CGSize winSize = [[CCDirector sharedDirector] winSize];

    [CCMenuItemFont setFontSize:20];

    [CCMenuItemFont setFontName:@"Arial"];

    

    CCMenuItemFont *startItem = [CCMenuItemFont itemWithString:@"开始游戏" block:^(id sender)

                                 {

                                     _isGameStarted = YES;

                                     CCMenuItem *item = (CCMenuItemFont*)sender;

                                     item.visible = NO;

                                     

                                     //6.spawn enemy after 1.0 sec

                                     [self performSelector:@selector(spawnEnemy)

                                                withObject:nil

                                                afterDelay:1.0f];

                                     

                                     //7.enable accelerometer

                                     self.isAccelerometerEnabled = YES;

                                     //9.enable touch

                                     self.isTouchEnabled = YES;

                                 }];

    startItem.position = ccp(winSize.width / 2, -winSize.height / 2);

    _startGameMenu = [CCMenu menuWithItems:startItem, nil];

    _startGameMenu.position = CGPointZero;

    [self addChild:_startGameMenu];

    

    

    //7 基本动作   从原来的位置移动到新的位置

    id moveBy = [CCMoveBy actionWithDuration:1.0 position:ccp(0, winSize.height)];  //位置的移动

    

    [_startGameMenu runAction:moveBy]; //开始移动

    //8 和位置有关的基本动作

    

    

    

    

    

    //1 CCActionManager

    [[[CCDirector sharedDirector] actionManager ] pauseTarget:_startGameMenu];//暂停

    

    [self schedule:@selector(resumeStartMenuAction:) interval:1.0]; //等待十秒之后才能开始移动

    

    //2CCAction 抽象类,几乎所有的类都继承该类

    

    //3.CCFiniteTimeAction 该类为有限时间动作,包含CCActionInstant 瞬时动作 和CCActionInterval 区间动作,他们包含了很多不同的动作

    //4 CCRepaeatForever 无限重复的动作

    

    //5跟随节点的动作CCFollow .可以替代Camera

    //6 CCSpeed  更还节点动作的速度。

    // 7CCOrbitCamera 继承与CCActionCamera 。使用球坐标系围绕屏幕中心旋转摄像机的视角

    

    

    

    



}



- (void)resumeStartMenuAction:(ccTime)dt

{

    [self unschedule:_cmd];   //停止当前动作

    [[[CCDirector sharedDirector]actionManager] resumeTarget:_startGameMenu];



}

 


二,基本动作:和位置相关的基本动作

 

CGSize winSize = [[CCDirector sharedDirector] winSize];

        

        CCSprite *plane = [CCSprite spriteWithFile:@"hero_1.png"];

        

        plane.position = ccp(winSize.width/2, winSize.height/2);

        

        [self addChild:plane];

        

        //2 moveBy 让节点对象在一定时间内移动一定的像素值  ,这个是相对于原位置的

        [plane runAction:[CCMoveBy actionWithDuration:3 position:ccp(50, 0)]];



        //3 CCjumpTo

        [plane runAction:[CCJumpTo actionWithDuration:4 position:ccp(winSize.width, winSize.height) height:40 jumps:3]];

        

        //4CCJumpBy 相对于原位置的移动 ,相对于原位置的偏移量为  100,0

        [plane runAction:[CCJumpBy actionWithDuration:3 position:ccp(100, 0) height:100 jumps:4]];

        

        //5CCBezier TO

        ccBezierConfig c = {ccp(300, 100),ccp(50, 50),ccp(-50, -50)};

        //1.参数一 相对于原位置的偏移,2,3分别是贝塞尔曲线的两个控制点

        [plane runAction:[CCBezierTo actionWithDuration:3 bezier:c]];

        

        

         //6 CCplace 将对象直接放到指定的位置

        [plane runAction:[CCPlace actionWithPosition:ccp(10, 10)]];


三,和大小相关的基本动作

 

 

 //二 和大小相关的基本动作

        // 1 CCScale TO、放大为原来的两倍

        [plane runAction:[CCScaleTo actionWithDuration:2 scale:2]];

        //2 CCScale By 缩小相应的倍数

        [plane runAction:[CCScaleBy actionWithDuration:3 scale:3]];

        [plane runAction:[CCScaleBy actionWithDuration:3 scaleX:0.5 scaleY:0.5]]; //对象的高度和宽度的缩小


四:和旋转有关的基本动作

 

 

 //三 和旋转有关的动作

        //1 CCRotate To  ,by ,超过180逆时针旋转,《180.顺时针旋转

        [plane runAction:[CCRotateTo actionWithDuration:2 angle:290]];


五,和显示有关的基本动作

 

 

  //四 和显示有关的动作

        //1 CCSHOW

       [plane runAction:[CCShow action]];

        //2CCHide

        [plane runAction:[CCHide action]];

        //3 切换uixiang的可视属性

	    [plane runAction:[CCToggleVisibility action]];

        //4 闪动一定的次数

        [plane runAction:[CCBlink actionWithDuration:5 blinks:10]];


六,和透明度有关的基本动作

 

 

//五 和透明度有关的动作

        //1 CCFadeIn  透明度从0 -》 255

        [plane runAction:[CCFadeIn actionWithDuration:8]];

        //2 CCFadeOut 透明度 从255 - 》0

        [plane runAction:[CCFadeOut actionWithDuration:5]];

        //3 CCFadeto

        [plane runAction:[CCFadeTo actionWithDuration:4 opacity:90]];


七,和色彩有关的基本动作

 

 

 //六  和色彩有关的动作

        //1 CCTintTO 在一定时间内将节点着色到某个特定的RGB值

        [plane runAction:[CCTintTo actionWithDuration:3 red:100 green:0 blue:1]];

        

        //2  CCTintBy 大致相同


八和翻转相关的基本动作

 //七 和翻转有关的动作

        //1 CCFlipX ,CCFlipY

    [plane runAction:[CCFlipX actionWithFlipX:YES]];


2.组合动作

 

一,组合动作的类

 

//八 组合动作

        //1 Spawn 动作数据,

        [plane runAction:[CCSpawn actions:[CCFadeOut actionWithDuration:4],[CCScaleTo actionWithDuration:3 scale:2], nil]];

        

       // 2 CCSequence 该组合可以按顺序执行多个动作,也可以包含多个组合动作

        

        //九  Ease 动作 使用ease动作修改的是节点内部动作的速度,而不是动作的执行时间

        

        id easeAction = [CCEaseBackOut actionWithAction:[CCMoveTo actionWithDuration:2 position:ccp(100+winSize.width/2, winSize.height/2)]];

      

        id action = [CCSequence actions:easeAction,[CCScaleTo actionWithDuration:2 scale:2], nil];

        

        [plane runAction:action];


二,一个飞机俯冲的组合动作的代码

 

 

#pragma mark - private methods

-(void) spawnEnemy{

    //1.

    CGSize winSize = [CCDirector sharedDirector].winSize;

    CCSprite *enemySprite = [self getAvailableEnemySprite];

    

    enemySprite.visible = YES;

    enemySprite.position = ccp( arc4random() % (int)(winSize.width - enemySprite.contentSize.width) + enemySprite.contentSize.width/2 , winSize.height + enemySprite.contentSize.height + 10);

    

    //2.

    float durationTime = arc4random() % 4 + 1;

    id moveBy = [CCMoveBy actionWithDuration:durationTime 

                                position:ccp(0,-enemySprite.position.y-enemySprite.contentSize.height)];

    

    int choice = arc4random() % 4 + 1;

    id easeMoveBy = nil;

    switch (choice) {

        case 1:

            easeMoveBy = [CCEaseIn actionWithAction:moveBy];

            break;

        case 2:

            easeMoveBy = [CCEaseExponentialIn actionWithAction:moveBy];

            break;

        case 3:

            easeMoveBy = [CCEaseBounceIn actionWithAction:moveBy];

            break;

        case 4:

            easeMoveBy = [CCEaseBackIn actionWithAction:moveBy];

            break;

        default:

            break;

    }

    

    id callback = [CCCallBlockN actionWithBlock:^(id sender)

    {

        CCSprite *sp = (CCSprite*)sender;

        sp.visible = NO;

        sp.position = ccp(0,winSize.height + sp.contentSize.height + 10);

        CCLOG(@"reset enemy plane!");

    }];

    id action = [CCSequence actions:easeMoveBy,callback, nil];

    

    

    CCLOG(@"enemySprite x = %f, y = %f",enemySprite.position.x, enemySprite.position.y);

    [enemySprite runAction:action];

    

    //3.

    [self performSelector:_cmd withObject:nil afterDelay:arc4random()%3 + 1];

    

}


三,延迟动作

 

 

   //十 延迟动作

        id action = [CCSequence actions:[CCDelayTime actionWithDuration:3],[CCMoveTo actionWithDuration:2 position:ccp(100 + winSize.width/2, winSize.height)], nil];

        

        [plane runAction:action];


 


 

你可能感兴趣的:(cocos2d)