cocos2d-x 3.0 Action,cocos2d-xaction

來源:互聯網
上載者:User

cocos2d-x 3.0 Action,cocos2d-xaction

***************************************轉載請註明出處:http://blog.csdn.net/lttree********************************************




題外話:

好久,好久,好久,好久,木有更新了 

8月份,去韓國旅遊了9天,回來,直接去井岡山參加實踐活動(課題是做一個安卓應用)

恩,現在就是在井岡山實踐基地,要19號才能回去。

而且,終於終於連上WIFI,可以發篇部落格了。



本文:

這次是關於cocos2d-x的Action,就是動作啦,各種動作,這個的學習,最好根據cocos2d-x的範例。

{

PS:範例程式的開啟方法:

用VS2012  開啟項目:  

->cocos2d-x檔案夾下

——build檔案夾下

cocos2d-win32.vc2012項目

}



本篇博文,通過七個測試程式來展示一些基本的Action:

一、Manual Transformation————對精靈的基本設定

二、MoveTo和MoveBy————精靈的基本動作(順序執行動作)

三、Span+Rotate————精靈的基本動作(同時執行動作)

四、DelayTime:m+delay+m————動作的順延強制

五、Repeat和RepeatForever————重複執行

六、Follow Action————跟隨動作

七、Sequence of InstantActions————結束機制



恩,可以邊運行著程式,邊看著代碼,觀摩著學習

檔案可以看ActionTest.cpp

在啟動並執行程式中,選擇Action—Basic觀看






——第一個測試——

Manual Transformation


它的相應代碼位置在:

//------------------------------------------------------------------//// ActionManual////------------------------------------------------------------------void ActionManual::onEnter(){    ActionsDemo::onEnter();    auto s = Director::getInstance()->getWinSize();    _tamara->setScaleX( 2.5f);    _tamara->setScaleY( -1.0f);    _tamara->setPosition( Point(100,70) );    _tamara->setOpacity( 128);    _grossini->setRotation( 120);    _grossini->setPosition( Point(s.width/2, s.height/2));    _grossini->setColor( Color3B( 255,0,0));    _kathia->setPosition( Point(s.width-100, s.height/2));    _kathia->setColor( Color3B::BLUE);}std::string ActionManual::subtitle() const{    return "Manual Transformation";}


可以從代碼中看到,

對對象進行的一系列操作:

//------------------------------------------------------------------//// ActionManual////------------------------------------------------------------------void ActionManual::onEnter(){    ActionsDemo::onEnter();// 獲得以點為單位的OpenGL視圖的大小    auto s = Director::getInstance()->getWinSize();// 對第一個對象進行操作:// 對X軸縮放    _tamara->setScaleX( 2.5f);    // 對Y軸縮放_tamara->setScaleY( -1.0f);    // 設定座標_tamara->setPosition( Point(100,70) );    // 設定透明度_tamara->setOpacity( 128);// 對第二個對象進行的操作:// 設定旋轉角度    _grossini->setRotation( 120);    _grossini->setPosition( Point(s.width/2, s.height/2));// 設定顏色    _grossini->setColor( Color3B( 255,0,0));<span style="white-space:pre"></span>// 對第三個對象進行的操作:    _kathia->setPosition( Point(s.width-100, s.height/2));    _kathia->setColor( Color3B::BLUE);}std::string ActionManual::subtitle() const{    return "Manual Transformation";}


或許你注意到了,這裡的函數是OnEnter,為什麼不是Init呢?

這裡就要說一下OnEnter和Init的區別:

Init:對象初始化時執行的函數。

OnEnter:當對象顯示出來的時候,開始執行的函數。(可見的時候才被執行)




——第二個測試——

第二個測試是關於MoveTo和MoveBy的測試

代碼:

//------------------------------------------------------------------////    ActionMove////------------------------------------------------------------------void ActionMove::onEnter(){    ActionsDemo::onEnter();    centerSprites(3);    auto s = Director::getInstance()->getWinSize();// 設定幾個動作:// 移動到一個絕對的座標點,第一個參數是花費的時間    auto actionTo = MoveTo::create(2, Point(s.width-40, s.height-40));    // 移動到一個相對座標點,即以本位置為基準向右80,向上80,第一個參數為花費的時間auto actionBy = MoveBy::create(2, Point(80,80));    // 做一個actionBy的反向動作auto actionByBack = actionBy->reverse();// 給三個精靈安排分別執行的動作// 第一個精靈 執行actionTo動作    _tamara->runAction( actionTo);    // 第二個 順序化執行actionBy和actionByBack動作(先執行actionBy,再執行actionByBack)_grossini->runAction( Sequence::create(actionBy, actionByBack, NULL));    // 第三個精靈執行MoveTo動作(內容:用1秒鐘,向上,向右移動40)_kathia->runAction(MoveTo::create(1, Point(40,40)));}std::string ActionMove::subtitle() const{    return "MoveTo / MoveBy";}

此處要特別注意的就是  Sequence,這個可以順序執行一系列動作。


接下來的都可以自己查看,

藉助API,很容易就能明白了。





——第三個測試程式——

這個測試程式是  Span+Rotate

//------------------------------------------------------------------//// ActionSpawn////------------------------------------------------------------------void ActionSpawn::onEnter(){    ActionsDemo::onEnter();    alignSpritesLeft(1);    auto action = Spawn::create(        JumpBy::create(2, Point(300,0), 50, 4),        RotateBy::create( 2,  720),        NULL);    _grossini->runAction(action);}std::string ActionSpawn::subtitle() const{    return "Spawn: Jump + Rotate";}

這個,就是讓兩個動作同時執行。

JumpBy::create(2,Point(300,0),50,4)

跳躍的語句。

RotateBy::create(2,720)

翻轉語句。

類似於Sequence,Spawn可以用來同時進行多個動作。




——第四個測試程式——

這個是 DelayTime:m+delay+m

就是中間加一個延遲時間:

//------------------------------------------------------------------//// ActionDelayTime////------------------------------------------------------------------void ActionDelayTime::onEnter(){    ActionsDemo::onEnter();    alignSpritesLeft(1);    auto move = MoveBy::create(1, Point(150,0));    auto action = Sequence::create( move, DelayTime::create(2), move, NULL);    _grossini->runAction(action);}std::string ActionDelayTime::subtitle() const{    return "DelayTime: m + delay + m";}

很明顯,用的是Sequence,順序執行,中間加一個延遲語句DelayTime即可。




——第五個測試程式——

Repeat和RepeatForever

//------------------------------------------------------------------//// ActionRepeat////------------------------------------------------------------------void ActionRepeat::onEnter(){    ActionsDemo::onEnter();    alignSpritesLeft(2);    auto a1 = MoveBy::create(1, Point(150,0));    auto action1 = Repeat::create(        Sequence::create( Place::create(Point(60,60)), a1, NULL) ,         3);     auto  action2 = RepeatForever::create(        Sequence::create(a1->clone(), a1->reverse(), NULL)        );    _kathia->runAction(action1);    _tamara->runAction(action2);}std::string ActionRepeat::subtitle() const{    return "Repeat / RepeatForever actions";}

Repeat執行的是一定次數的動作。

Repeat::create中有兩個參數:第一個是需要重複的動作,第二個重複的次數。

重複的次數取值範圍是1到2^30之間的不帶正負號的整數。

而相應的RepeatForever就沒有次數的參數,只有一個參數。




——第六個測試程式——

Follow Action

//------------------------------------------------------------------//// ActionFollow////------------------------------------------------------------------void ActionFollow::onEnter(){    ActionsDemo::onEnter();    centerSprites(1);    auto s = Director::getInstance()->getWinSize();// 給精靈設定座標位置    _grossini->setPosition(Point(-200, s.height / 2));// 給精靈設定動作    auto move = MoveBy::create(2, Point(s.width * 3, 0));    auto move_back = move->reverse();    // 設定動作的順序auto seq = Sequence::create(move, move_back, NULL);    // 設定 動作永遠重複auto rep = RepeatForever::create(seq);    _grossini->runAction(rep);// 設定跟隨的語句,creat有兩個參數,第一個是所需要跟隨的精靈,第二個參數是動作的邊界    this->runAction(Follow::create(_grossini, Rect(0, 0, s.width * 2 - 100, s.height)));}

設定跟隨的語句中,第二個參數是關於動作的邊界,當參數設定為Rect::ZERO時,即為沒有邊界。




——第七個測試程式——

Sequence of InstantActions

//------------------------------------------------------------------////    ActionSequence2////------------------------------------------------------------------void ActionSequence2::onEnter(){    ActionsDemo::onEnter();    alignSpritesLeft(1);// 設定節點是否可見,false為不可見    _grossini->setVisible(false);// 設定  順序 展示的動作    auto action = Sequence::create(Place::create(Point(200,200)),Show::create(),MoveBy::create(1, Point(100,0)),// 當上述三個動作結束後,調用下面這三個CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback1,this)),CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback2,this,_grossini)),CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback3,this,_grossini,0xbebabeba)),NULL);    _grossini->runAction(action);}

CallFunc::create語句中,

CC_CALLBACK_0表示沒有參數,Action使用時應用

CC_CALLBACK_1表示只有一個參數,Menu使用時應用

CC_CALLBACK_2表示綁定有兩個參數,一般用於使用者的單點觸摸,多點觸摸



******總結一下******


Action:

——基本動作

MoveTo/MoveBy

RoatTo/RoatBy

JumpTo/JumpBy

.......

——組合動作

Sequence  順序執行

Spawn同時執行

——動作的取反

Reverse

——動作的重複執行

Repeat

RepeatForever

——函數動作

CallFucn::create()


恩,就是到這裡啦~




參考資料:沈老師


***************************************轉載請註明出處:http://blog.csdn.net/lttree********************************************


cocos2d-x Sprite Action 問題

#define THETAG 123/*這數字隨你定*/
初始化動作:
CCAction*pAction=你要執行的動作;
pAction->setTag(THETAG);
pSprite->runAction(pAction); //你原本執行的動作
加速:
pSprite->stopActionByTag(THETAG);
pAction=CCSpeed::actionWithAction(
你要執行的動作/*注意時間這裡要設定為20秒,因為加速實際就是十秒執行20秒的動作*/,
2/*如果要減速就是0.7*/);
pAction->setTag(THETAG);
pSprite->runAction(CCSequence::actions(
pAction,
CCCallFunc::actionWithTarget(pSprite,callfunc_selector(回呼函數)),
NULL

));

下面是回呼函數內部://這函數為執行動作的精靈的成員函數
stopActionByTag(THETAG);
CCAction*pAction=你要執行的動作;
pAction->setTag(THETAG);
runAction(pAction);
 
cocos2d-x “actionWithDuration”: 不是“cocos2d::CCMoveTo”的成員??

2.0中好多函數都改名為create了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.