2014-12-06 106 views
0

你好我正在做一個項目。我想知道如何製作我的角色。我遵循cocos wiki中的指南,但是我無法在我的代碼中實現。Cocos2d-x動畫

我的角色可以移動和走路,我想在他跳躍時應用動畫。它有一個onKeyPressed方法。我不知道如何改變正常的精靈到運動spritesheet,我有plist,但我不知道如何加載我的代碼。

我嘗試了很多教程,但我不知道如何在我的項目中實現它們。

回答

0

我寫了一個簡單的播放器動畫和動畫教程,你可以找到它here。 您還可以獲取此here的完整源代碼。

但是本教程不使用任何spritesheet,我儘量保持它儘可能簡單。要使用spritesheets,你需要做一些修改:

在你的場景的「init()」方法中,你需要添加一個SpriteBatchNode對象並將plist文件添加到SpriteFrameCache單例中。

auto spriteBatch = SpriteBatchNode::create("sprites/sprite_sheet.png", 200); 
auto spriteFrameCache = SpriteFrameCache::getInstance(); 
spriteFrameCache->addSpriteFramesWithFile("sprites/sprite_sheet.plist"); 
this->addChild(spriteBatch, kMiddleground); 

那麼玩家對象添加到SpriteBatchNode

player = Player::create(); 
spriteBatch->addChild(player); 

然後在播放器類,創建這樣的動畫:

SpriteFrameCache* cache = SpriteFrameCache::getInstance(); 
Vector<SpriteFrame*> moveAnimFrames(10); // parameter = number of frames you have for this anim 
char str[100] = {0}; 

for(int i = 0; i < 10; i++) // this 10 is again the number of frames 
{ 
    sprintf(str, "move_%d.png", i); 
    SpriteFrame* frame = cache->getSpriteFrameByName(str); 
    moveAnimFrames.pushBack(frame); 
} 

moveAnimation = Animation::createWithSpriteFrames(moveAnimFrames, 0.011f); 
moveAnimation->setLoops(-1); 
moveAnimate = Animate::create(moveAnimation); 
moveAnimate->retain(); // retain so you can use it again 
this->runAction(moveAnimate); // run the animation 

您可以創建不同的動畫,並通過改變他們:

this->stopAction(moveAnimate); 
this->runAction(jumpAnimate); 

希望這有助於。