cocos2d-x學習之行走動作

很多遊戲中都有各種精靈行走的動作,像RPG類的和動作類的遊戲就經常見到主角行走的動作。現在我們來看看它們是怎樣實現人物行走。

學習這個之前,你首先要對cocos2d-x動作類有一個基礎的認識。不知道的童鞋就先去學習一下相關知識吧.

好了,如果你瞭解過cocos2d-x動作類的話,接下來就可以跟着我完成一個人物行走的的實現。

首先人物行走肯定是從一個地方移動到一個地方 那我們就要用到CCMoveBy類來實現人物的移動,
其次,人物在行走的時候是有動作的,腳的擺動,手的擺動等。這樣我們就需要創建一個動畫。

根據這兩點,我們來實現人物行走:

(1)人物移動:
    
     CCPoint moveByPosition;         

      switch(moveDirection)              //根據moveDirection的值來選擇上下左右
     {
       case up:
         moveByPosition = ccp(0,10);
break;
       case down:
         moveByPosition = ccp(0,-10);
break;
       case left:
         moveByPosition = ccp(-10,0);
break;
       case right:
         moveByPosition = ccp(10,0);
break;
       default:
         moveByPosition = ccp(0,0); 
break;
     }

     CCMoveBy *moveBy = CCMoveBy::create(0.3f,moveByPosition);//創建移動動作




(2)行走動畫:

     創建動畫可以用紋理圖片來創建,也可以用單獨的圖片來創建。


紋理圖片創建:


   //將圖片生成紋理,保存到全局的紋理緩存取
    CCTexture2D *heroTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png");
   //用紋理創建4幅幀動畫,第二個參數表示紋理圖片區域的x,y,width,height,根據direction的不同分別爲向上、下、左、右移動的動畫
    CCSpriteFrame *frame0,*frame1,*frame2,*frame3;

    frame0=CCSpriteFrame::createWithTexture(heroTexture,cocos2d::CCRectMake(Rect*0,Rect*direction,Rect,Rect));
    frame1=CCSpriteFrame::createWithTexture(heroTexture,cocos2d::CCRectMake(Rect*1,Rect*direction,Rect,Rect));
    frame2=CCSpriteFrame::createWithTexture(heroTexture,cocos2d::CCRectMake(Rect*2,Rect*direction,Rect,Rect));
    frame3=CCSpriteFrame::createWithTexture(heroTexture,cocos2d::CCRectMake(Rect*3,Rect*direction,Rect,Rect));

    CCArray *animFrames=CCArray::create();
    animFrames->addObject(frame0);
    animFrames->addObject(frame1);
    animFrames->addObject(frame2);
    animFrames->addObject(frame3);
    //根據4幅幀生成CCAnimation對象
    CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames);




根據單張圖片來創建動畫:
/*****************************************************************************
    CCAnimation *animation = CCAnimation::create();
    
    //根據direction的不同分別爲向上、下、左、右移動的動畫
    string Image1 = direction+"player1.png";
    string Image2 = direction+"player1.png";
    string Image3 = direction+"player1.png";
    string Image4 = direction+"player1.png";


    animation->addSpriteFrameWithFileName(Image1.c_str());
    animation->addSpriteFrameWithFileName(Image2.c_str());
    animation->addSpriteFrameWithFileName(Image3.c_str());
    animation->addSpriteFrameWithFileName(Image4.c_str());


   //設置動畫播放的屬性,每一幀的時間0.3f
    animation->setDelayPerUnit(0.3f);
   animation->setRestoreOriginalFrame(true);

***********************************************************************************/


根據移動和行走動畫來創建動畫動作:

 CCAction *action = CCSequence::create(
                                           CCSpawn::create(                    //兩個動作同步進行
                                                           CCAnimate::create(animation),
                                                           moveBy,
                                                           NULL
                                                           ),
                                           // function         //CCCallFuncN,CCCallFunc,CCCallFuncND創建函數調用
                                           NULL
                                       );
   
   //最後,讓hero精靈執行這個動作
hero->runAction(action);


  這樣人物行走的動作功能就實現了。如果開發者還想在精靈完成動作後執行某些功能,可以用
  CCCallFuncN,CCCallFunc,CCCallFuncND創建函數調用放在最後。這樣等精力完成動作後就會
  自動調用創建的函數實現某些功能了





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章