[Cocos2d-x For WP8]Action 常用动作

    Action相当于是Cocos2d-x里面的动画操作,在Cocos2d-x里面的动画基类是CCAction类,从CCAction类派生出来的就有很多常用的动作的实现类,利用这些类就可以给我们游戏的精灵实现丰富的动作效果,类似于我们在WP8里面使用各种各样的动画类实现动画效果一样。那么下面就是这些类的结构图:

官方的技术文档可以参考下面的地址:

http://www.cocos2d-x.org/reference/native-cpp/d7/d12/classcocos2d_1_1_c_c_action.html

那么从上图可以看到Cocos2d-x提供了很多基本的动作类,主要包括两大类:一类是瞬时动作(CCActionInstant),一类是延时动作(CCActionInterval)。

瞬时动作CCActionInstanse是没有执行过程,动作瞬间就执行完成了;CCActionInterval则是执行需要一个过程,跟我们编写的普通程序的动画一样的原理。下面我们主要介绍下延时动作,延时动作有下面这些:移动(CCMoveTo/CCMoveBy)、缩放(CCScaleTo/CCScaleBy)、旋转(CCRotateTO/CCRotateBy)、扭曲(CCSkewTo/CCSkewBy)、跳跃(CCJumpTo/CCJumpBy)、贝塞尔曲线(CCBezierTo/CCBezierBy)、闪烁(CCBink)、淡入淡出(CCFadeIn/CCFadeOut)、染色(CCTintTo/CCTintBy)等,我们也可以把上面这些动作的几个组合成一个序列来执行,多种效果混合在一起。

下面是一些代码的实例:

//在init方法里面初始化当前的实例
bool RotateWorldTest::init()
{
    bool bRet = false;

    do 
    {
        //CCLayer进行初始化,初始化失败跳出循环
        if ( !CCLayer::init() )
        {
            break;
        }
        //获取手机屏幕的大小
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        //创建图片精灵
        CCSprite *sprite = CCSprite::create("cat.png");
        sprite->setPosition(ccp(size.width * 0.5, size.height * 0.5));
        sprite->setScale(1.5f);
        this->addChild(sprite);
        //移动
        //从当前的位置到新的位置
        CCActionInterval*  actionMoveTo = CCMoveTo::create(2, ccp(size.width-40, size.height-40));
        sprite->runAction( actionMoveTo);
        //从当前的位置递增到另外的位置,然后再返回
        CCActionInterval*  actionMoveBy = CCMoveBy::create(2, ccp(80,80));
        CCActionInterval*  actionMoveByBack = actionMoveBy->reverse();
        sprite->runAction( CCSequence::create(actionMoveBy, actionMoveByBack, NULL));

        //缩放
        CCActionInterval*  actionScaleTo = CCScaleTo::create(2.0f, 0.5f);
        sprite->runAction( actionScaleTo);
        CCActionInterval*  actionScaleBy = CCScaleBy::create(2.0f, 1.0f, 10.0f);
        sprite->runAction( CCSequence::create(actionScaleBy, actionScaleBy->reverse(), NULL));

        //倾斜
        CCActionInterval *actionSkewTo = CCSkewTo::create(2, 37.2f, -37.2f);
        sprite->runAction( actionSkewTo);
        CCActionInterval *actionSkewBy = CCSkewBy::create(2, 0.0f, -90.0f);
        sprite->runAction(actionSkewBy);

         //旋转
        CCRotateTo* actionRotateTo = CCRotateTo::create(2, 37.2f, -37.2f);
        sprite->runAction( actionRotateTo);
        CCRotateBy* actionRotateBy = CCRotateBy::create(2, 0.0f, -90.0f);
        sprite->runAction( actionRotateBy);

        //跳跃
        CCActionInterval*  actionJumpTo = CCJumpTo::create(2, ccp(300,300), 50, 4);
        sprite->runAction( actionJumpTo);
        CCActionInterval*  actionJumpBy = CCJumpBy::create(2, ccp(300,0), 50, 4);
        CCActionInterval*  actionJumpByBack = actionJumpBy->reverse();
        sprite->runAction( CCSequence::create(actionJumpBy, actionJumpByBack, NULL));
        CCActionInterval*  actionJumpUp = CCJumpBy::create(2, ccp(0,0), 80, 4);
        sprite->runAction( CCRepeatForever::create(actionJumpUp));

        //贝塞尔曲线运动
        ccBezierConfig bezier;
        bezier.controlPoint_1 = ccp(0, size.height/2);
        bezier.controlPoint_2 = ccp(300, -size.height/2);
        bezier.endPosition = ccp(300,100);
        CCActionInterval*  bezierForward = CCBezierBy::create(3, bezier);
        CCActionInterval*  bezierBack = bezierForward->reverse();    
        CCAction*  rep = CCRepeatForever::create(CCSequence::create( bezierForward, bezierBack, NULL));
        sprite->runAction( rep);

        sprite->setPosition(ccp(80,160));
        ccBezierConfig bezier2;
        bezier2.controlPoint_1 = ccp(100, size.height/2);
        bezier2.controlPoint_2 = ccp(200, -size.height/2);
        bezier2.endPosition = ccp(240,160);
        CCActionInterval*  bezierTo1 = CCBezierTo::create(2, bezier2);    
        sprite->runAction(bezierTo1);

        sprite->setPosition(ccp(400,160));
        CCActionInterval*  bezierTo2 = CCBezierTo::create(2, bezier2);
        sprite->runAction(bezierTo2);

        //闪烁效果
        CCActionInterval*  actionBlink1 = CCBlink::create(2, 10);
        sprite->runAction( actionBlink1);

        //淡出淡入
        CCActionInterval*  actionFadeIn1 = CCFadeIn::create(1.0f);
        CCActionInterval*  action1FadeInBack = actionFadeIn1->reverse();
        sprite->runAction( CCSequence::create( actionFadeIn1, action1FadeInBack, NULL));
        CCActionInterval*  actionFadeOut2 = CCFadeOut::create(1.0f);
        CCActionInterval*  actionFadeOut2Back = actionFadeOut2->reverse();
        sprite->runAction( CCSequence::create( actionFadeOut2, actionFadeOut2Back, NULL));

        //色彩
        CCActionInterval*  actionTint1 = CCTintTo::create(2, 255, 0, 255);
        sprite->runAction( actionTint1);
        CCActionInterval*  actionTint2 = CCTintBy::create(2, -127, -255, -127);
        CCActionInterval*  actionTint2Back = actionTint2->reverse();
        sprite->runAction( CCSequence::create( actionTint2, actionTint2Back, NULL));
    
        //接收界面的的触摸事件
        setTouchEnabled(true);

        bRet = true;
    } while (0);
    //返回成功
    return bRet;
}

把代码复制到Hello World项目里面,就可以看到相关的运行效果了。

 

posted on 2013-08-19 22:10  linzheng  阅读(1144)  评论(3编辑  收藏  举报

导航