Я написал простое руководство по движению игроков и анимации, вы можете найти его here. Вы также можете получить весь исходный код для этого here.
Однако этот учебник не использует каких-либо спрайтов, я старался максимально упростить его. Чтобы использовать спрайты, вам нужно будет внести некоторые изменения:
В методе «init()» вашей сцены вам нужно добавить объект SpriteBatchNode и добавить файл plist в singleton 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);
Надеюсь, это поможет.