【问题标题】:Cocos2d-x animationCocos2d-x 动画
【发布时间】:2015-02-04 17:38:36
【问题描述】:

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

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

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

【问题讨论】:

    标签: animation sprite sprite-sheet cocos2d-x-2.x


    【解决方案1】:

    我写了一个简单的玩家动作和动画教程,你可以找到它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);
    

    希望这会有所帮助。

    【讨论】:

    • 知道如何在精灵水平翻转的情况下运行动画吗?我尝试做 mySprite->setFlippedX(true),但它似乎没有翻转动画精灵......有什么想法吗?我一直在努力解决这个问题......
    猜你喜欢
    • 1970-01-01
    • 2020-07-12
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2010-12-18
    • 2011-03-30
    相关资源
    最近更新 更多