[cocos2d-x篇]Sprite移動

cocos2dx使用版本︰v 2.0.4
cocosBuilder使用版本︰v 2.0 alpha 1
本文貼自JaoYE部落格
声明:博文内容为自己整理笔记所用,有不对的地方还请大家指正.
在cocos2dx 中的tests 项目里 已经带了 足够多的 动画事例
ActionsTest
我只是看了其中一些 自我感觉比较常用的动画
ActionMove   移动
在cocos2d-x中移动分  to  和   by   两种
1     CCSize s = CCDirector::sharedDirector()->getWinSize();
2 
3     CCActionInterval*  actionTo = CCMoveTo::create(2, CCPointMake(s.width-40, s.height-40));    //2s的时间移动到某个位置相对于屏幕
4     CCActionInterval*  actionBy = CCMoveBy::create(2, CCPointMake(80,80));                        //2s的时间移动到80,80
5     CCActionInterval*  actionByBack = actionBy->reverse();                                        //让动作回复到actionBy之前的地方
6 
7     m_tamara->runAction( actionTo);                                                                //执行actionTo
8     m_grossini->runAction( CCSequence::create(actionBy, actionByBack, NULL));                    //顺序执行(actionBy,actionByBack),必须用NULL结尾
9     m_kathia->runAction(CCMoveTo::create(1, CCPointMake(40,40)));                                //1s的时间移动到40,40
 ActionScale  缩放
同样分为 to 和 by 记得有地方说这两种缩放  效果会不一样  但是我在win32 上实验   缩放大小是一样的,以后在到xcode 上实验
1     CCActionInterval*  actionTo = CCScaleTo::create(2.0f, 0.5f);                        //2个参数(时间,放大倍数)
2     CCActionInterval*  actionBy = CCScaleBy::create(2.0f, 1.0f, 10.0f);                 //三个参数(时间,x方向放大倍数,y方向放大倍数)
3     CCActionInterval*  actionBy2 = CCScaleBy::create(2.0f, 5.0f, 1.0f);                    
4 
5     m_grossini->runAction( actionTo);                                                
6     m_tamara->runAction( CCSequence::create(actionBy, actionBy->reverse(), NULL));        
7     m_kathia->runAction( CCSequence::create(actionBy2, actionBy2->reverse(), NULL));    
ActionSkew
还没研究 暂时留在这里
 1     CCActionInterval *actionTo = CCSkewTo::create(2, 37.2f, -37.2f);
 2     CCActionInterval *actionToBack = CCSkewTo::create(2, 0, 0);
 3     CCActionInterval *actionBy = CCSkewBy::create(2, 0.0f, -90.0f);
 4     CCActionInterval *actionBy2 = CCSkewBy::create(2, 45.0f, 45.0f);
 5     CCActionInterval *actionByBack = actionBy->reverse();
 6 
 7     m_tamara->runAction(CCSequence::create(actionTo, actionToBack, NULL));
 8     m_grossini->runAction(CCSequence::create(actionBy, actionByBack, NULL));
 9 
10     m_kathia->runAction(CCSequence::create(actionBy2, actionBy2->reverse(), NULL));
ActionRotate
 旋转
 1     CCActionInterval*  actionTo = CCRotateTo::create( 2, 45);                        //2s内顺时针旋转45度(每跨度180,旋转方向会相反)
 2     CCActionInterval*  actionTo2 = CCRotateTo::create( 2, -45);                        //2s内逆时针旋转45度(每跨度180,旋转方向会相反)
 3     CCActionInterval*  actionTo0 = CCRotateTo::create(2 , 0);
 4     m_tamara->runAction( CCSequence::create(actionTo, actionTo0, NULL));
 5 
 6     CCActionInterval*  actionBy = CCRotateBy::create(2 ,  360);                        //2s内自身旋转360度,正数为顺时针旋转,负数相反
 7     CCActionInterval*  actionByBack = actionBy->reverse();                            //回复
 8     m_grossini->runAction( CCSequence::create(actionBy, actionByBack, NULL));        
 9 
10     m_kathia->runAction( CCSequence::create(actionTo2, actionTo0->copy()->autorelease(), NULL));
11     //actionTo0->copy()->autorelease()  不清楚  作用 
ActionJump
跳动移动
1     CCActionInterval*  actionTo = CCJumpTo::create(2, CCPointMake(300,300), 50, 4);        //2s内跳向300,300的位置,跳跃高度为50,跳跃次数为4
2     CCActionInterval*  actionBy = CCJumpBy::create(2, CCPointMake(300,0), 50, 4);        //向x方向跳动300像素,跳跃高度为50,跳跃次数为4
3     CCActionInterval*  actionUp = CCJumpBy::create(2, CCPointMake(0,0), 80, 4);            //原地跳动 高度80 次数为4
4     CCActionInterval*  actionByBack = actionBy->reverse();                                //回复
5 
6     m_tamara->runAction( actionTo);
7     m_grossini->runAction( CCSequence::create(actionBy, actionByBack, NULL));            //顺序执行actionBy, actionByBack动作
8     m_kathia->runAction( CCRepeatForever::create(actionUp));                            //CCRepeatForever  一直重复相同的动作
顺序执行和并发执行
 1     CCMoveTo *moveTo = CCMoveTo::actionWithDuration(5, ccp(150,250));
 2     CCScaleTo *scaleTo = CCScaleTo::actionWithDuration(5, 3);
 3     CCDelayTime *waiting = CCDelayTime::actionWithDuration(2);//停滞2秒
 4     CCRotateBy *rotBy = CCRotateBy::actionWithDuration(5, 180);
 5     //顺序执行定义好的动画
 6     _sprite1->runAction(CCSequence::actions(moveTo,waiting,scaleTo,waiting,rotBy,NULL));
 7     //并发执行(同时执行)各类动画
 8     _sprite2->runAction(CCSpawn::actions(moveTo,scaleTo,rotBy));
 9     //一直重复相同的动作
10     _sprite3->runAction(CCRepeatForever::actionWithAction(rotBy));
_sprite->stopAction(scale);//停住某个action,只能停止单个动画,在组合动画中不能停住

带有回调方法的移动
ActionCallFunc   无参回调
 1 void ActionCallFunc::onEnter()
 2 {
 3     ActionsDemo::onEnter();
 4 
 5     centerSprites(3);
 6 
 7     CCFiniteTimeAction*  action = CCSequence::create(
 8         CCMoveBy::create(2, CCPointMake(200,0)),
 9         CCCallFunc::create(this, callfunc_selector(ActionCallFunc::callback1)), 
10         NULL);
11 
12     CCFiniteTimeAction*  action2 = CCSequence::create(
13         CCScaleBy::create(2 ,  2),
14         CCFadeOut::create(2),
15         CCCallFuncN::create(this, callfuncN_selector(ActionSequence2::callback2)), 
16         NULL);
17 
18     CCFiniteTimeAction*  action3 = CCSequence::create(
19         CCRotateBy::create(3 , 360),
20         CCFadeOut::create(2),
21         CCCallFuncND::create(this, callfuncND_selector(ActionSequence2::callback3), (void*)0xbebabeba), 
22         NULL);
23 
24     m_grossini->runAction(action);
25     m_tamara->runAction(action2);
26     m_kathia->runAction(action3);
27 }
28 
29 
30 void ActionCallFunc::callback1()
31 {
32     CCSize s = CCDirector::sharedDirector()->getWinSize();
33     CCLabelTTF *label = CCLabelTTF::create("callback 1 called", "Marker Felt", 16);
34     label->setPosition(CCPointMake( s.width/4*1,s.height/2));
35 
36     addChild(label);
37 }
38 
39 void ActionCallFunc::callback2(CCNode* pSender)
40 {
41     CCSize s = CCDirector::sharedDirector()->getWinSize();
42     CCLabelTTF *label = CCLabelTTF::create("callback 2 called", "Marker Felt", 16);
43     label->setPosition(CCPointMake( s.width/4*2,s.height/2));
44 
45     addChild(label);
46 }
47 
48 void ActionCallFunc::callback3(CCNode* pTarget, void* data)
49 {
50     CCSize s = CCDirector::sharedDirector()->getWinSize();
51     CCLabelTTF *label = CCLabelTTF::create("callback 3 called", "Marker Felt", 16);
52     label->setPosition(CCPointMake( s.width/4*3,s.height/2));
53     addChild(label);
54 }
55    

有参数的回调
 1 void ActionCallFuncND::onEnter()
 2 {
 3     ActionsDemo::onEnter();
 4 
 5     centerSprites(1);
 6 
 7     CCFiniteTimeAction* action = CCSequence::create(CCMoveBy::create(2.0f, ccp(200,0)),
 8         CCCallFuncND::create(this, callfuncND_selector(ActionCallFuncND::removeFromParentAndCleanup), (void*)true),
 9         NULL);
10 
11     m_grossini->runAction(action);
12 }
13 
14 std::string ActionCallFuncND::title()
15 {
16     return "CallFuncND + auto remove";
17 }
18 
19 std::string ActionCallFuncND::subtitle()
20 {
21     return "CallFuncND + removeFromParentAndCleanup. Grossini dissapears in 2s";
22 }
23 
24 void ActionCallFuncND::removeFromParentAndCleanup(CCNode* pSender, void* data)
25 {
26     bool bCleanUp = (bool)data;
27     m_grossini->removeFromParentAndCleanup(bCleanUp);
28 }
 其他动画可以在tests里自行查找
cocos2d-x提供丰富的动画

沒有留言 :

張貼留言