cocos2d-x v3精灵动画问题

时间:2014-09-07 08:42:40

标签: animation cocos2d-x

我有一个 cocos2d-x v3 Windows项目,我想要一个动画精灵。我在一个名为Worker的对象中的精灵与其他参数。为了运行动画,我尝试了以下操作,但它总是会出现分段错误:

//worker.cpp:

...

    void Worker::initAnimationWalk() {
        spritebatch = cocos2d::SpriteBatchNode::create("robo.png");
        auto cache = cocos2d::SpriteFrameCache::getInstance();
        cache->addSpriteFramesWithFile("robo.plist");
        worker = cocos2d::Sprite::createWithSpriteFrameName("robot1.png");
        //spritebatch->addChild(worker);

        Vector<SpriteFrame *> animFrames(3);

        char str[100] = {0};

        for(int i = 1; i <= 3; i++) 
        {
            sprintf(str, "robot%i.png", i);
            cocos2d::SpriteFrame* frame = cache->getSpriteFrameByName( str );
            animFrames.pushBack(frame);
        }

        animation = cocos2d::Animation::createWithSpriteFrames(animFrames, 0.2f);
    }

cocos2d::Animation *Worker::getAnimationWalk() {
    return animation;
}

...

//Worker.h

    #ifndef __WORKER_H__
    #define __WORKER_H__

    #include "cocos2d.h"
    class Worker
    {
    public:
        ...
        void initAnimationWalk();
        cocos2d::Animation *getAnimationWalk();
    private:
        cocos2d::Animation *animation;
        cocos2d::Sprite *worker;
        cocos2d::SpriteBatchNode *spritebatch;
    };

    #endif

//Now i want the animation in my scene: 
//HellowWorldScene.cpp

    void HelloWorld::setMyAction() { //all Worker Objects in an vector
        vector<Worker>::iterator it = workerVector.begin();
        for(int i = 0; i<workerVector.size();i++) {
        it->getWorker()->stopAllActions();
                                auto actionAnim = Animate::create(it->getAnimationWalk());
                                if(it->getWorker()->getPosition().x > 0.50*2*p.x) {
                                    auto action = cocos2d::MoveTo::create(it->getSpeed(),Point(0,it->getWorker()->getPosition().y));
                                    auto sp = Spawn::createWithTwoActions(action, actionAnim);
                                    it->getWorker()->runAction(sp);
                                    it->getWorker()->setFlipX(true);
                                } else {
                                    auto action = cocos2d::MoveTo::create(it->getSpeed(),Point(2*p.x,it->getWorker()->getPosition().y));
                                    auto sp = Spawn::createWithTwoActions(action, actionAnim);
                                    it->getWorker()->runAction(sp);
                                    it->getWorker()->setFlipX(false);
                                }
    }
    it++
    }

任何帮助?

2 个答案:

答案 0 :(得分:1)

SpriteAnimation使用动画自动更改图像。

Animation *animation2=Animation::create();
    animation2->setDelayPerUnit(0.7f);//Time Duration change Images 
    animation2->addSpriteFrameWithFile("img_1.png");
    animation2->addSpriteFrameWithFile("img_2.png");
    animation2->addSpriteFrameWithFile("img_3.png");

    AnimationCache::getInstance()->addAnimation(animation2, "animation2");

SpriteName-&gt; runAction(Sequence :: create(Animate :: create(animation2),NULL)); //此动画只有一次

          OR
    SpriteName->runAction(RepeatForever::create(Sequence::create(Animate::create(animation2),NULL)));//If Repeate this animation write it..

答案 1 :(得分:1)

SpriteAnimation auto change images using Animation.

Animation *anim=Animation::create();
    anim->setDelayPerUnit(0.7f);//Time Duration change Images 
    anim->addSpriteFrameWithFile("img_01.png");
    anim->addSpriteFrameWithFile("img_02.png");
    anim->addSpriteFrameWithFile("img_03.png");

    AnimationCache::getInstance()->addAnimation(anim, "anim");

SpriteNM->runAction(Sequence::create(Animate::create(anim),NULL));//this animation Only One time

          OR
    SpriteNM->runAction(RepeatForever::create(Sequence::create(Animate::create(anim),NULL)));//If Repeate this animation write it..