我写了一个简单的玩家动作和动画教程,你可以找到它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);
希望这会有所帮助。