0

你好我正在做一个项目。我想知道如何为我的角色设置动画。我按照 cocos wiki 中的指南进行操作,但无法在我的代码中实现。

我的角色可以移动和行走,我想在他跳跃时应用动画。它有一个onKeyPressed方法。我不知道如何将正常精灵更改为运动spritesheet,我有plist但我不知道如何加载我的代码。

我尝试了很多教程,但我不知道如何在我的项目中实现它们。

4

1 回答 1

0

我写了一个简单的玩家动作和动画教程,你可以在这里找到。您还可以在此处获取完整的源代码。

然而,本教程不使用任何精灵表,我尽量保持它尽可能简单。要使用 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);

希望这可以帮助。

于 2014-12-07T06:13:09.337 回答