精灵runAction Animation切换的问题

我在游戏初始化场景的时候添加一个精灵,然后我设置里一个idleAnimate()函数:

void Swordsman::idleAnimate() {
    CCLOG("Idle");
    _sprite->runAction(Animate::create(idleAnimation));
}


```


当我点击屏幕的我触发一个runAnimate()的函数:
void Swordsman::runAnimate() {
    _sprite->runAction(Animate::create(runAnimation));
}



```



可是这是这里就报我错了。这是怎么回事?

我希望的情形是他开始走动时播放runAnimation的精灵帧,不走时播放idleAnimation的精灵帧。
是不是精灵只能是设置一次runAction里面的Animate?还是设置的方法出错?

报什么错?代码能给完整点吗?

VS堆栈显示是在Animate::initAnimation中出错了

    if ( ActionInterval::initWithDuration(singleDuration * animation->getLoops() ) )
    {
        _nextFrame = 0;
        setAnimation(animation);
        _origFrame = nullptr;
        _executedLoops = 0;

        _splitTimes->reserve(animation->getFrames().size()); // 这里出错


```


swordsman.cpp
#include "cocos2d.h"
#include "Swordsman.h"
#include "Enemy.h"
USING_NS_CC;


Swordsman::Swordsman(const std::string str) : Enemy(str) {
    initPhysicsBody();
    initAnimate();
    idleAnimate();
}


void Swordsman::moveLeft() {
    speed = _physicsBody->getVelocity() + Vec2(-MOVE_VEC, 0);
    if (isSpeedZero()) {
        idleAnimate();
        isMove = false;
        return;
    }
    if (isFaceRight) {
        _sprite->setFlippedX(true);
        isFaceRight = false;
    }

    if (!isMove) {
        runAnimate(); // 执行这里 要么没有变化要么出错
        isMove = true;
    }
    _physicsBody->setVelocity(speed);
}

void Swordsman::moveRight() {

    
    speed = _physicsBody->getVelocity() + Vec2(MOVE_VEC, 0);
    if (isSpeedZero()) {
        idleAnimate();
        isMove = false;
        return;
    }
    if (!isFaceRight) {
        _sprite->setFlippedX(false);
        isFaceRight = true;
    }

    if (!isMove) {
        runAnimate(); // 执行这里 要么没有变化要么出错
        isMove = true;
    }

    _physicsBody->setVelocity(speed);
}





void Swordsman::idleAnimate() {
    _sprite->stopAllActions();
    _sprite->runAction(currentAnimate);
}

void Swordsman::runAnimate() {
    _sprite->stopAllActions();
    _sprite->runAction(Animate::create(runAnimation));
}

void Swordsman::attackAnimate() {
    currentAnimate = Animate::create(attackAnimation);
    _sprite->stopAllActions();

    _sprite->runAction(currentAnimate);
}




void Swordsman::initIdleAnimation() {
    idleAnimation = Animation::create();
    for (int i = 1; i < 5; i++) {
        idleAnimation->addSpriteFrameWithFile((std::string)StringUtils::format("Swordsman/idle_%d.png", i));
    }
    idleAnimation->setDelayPerUnit(0.18f);
    idleAnimation->setLoops(-1);
}

void Swordsman::initRunAnimation() {
    runAnimation = Animation::create();
    for (int i = 1; i < 5; i++) {
        runAnimation->addSpriteFrameWithFile((std::string)StringUtils::format("Swordsman/move_%d.png", i));
    }
    runAnimation->setDelayPerUnit(0.18f);
    runAnimation->setLoops(-1);
}

void Swordsman::initAttackAnimation() {
    attackAnimation = Animation::create();
    for (int i = 1; i < 5; i++) {
        attackAnimation->addSpriteFrameWithFile((std::string)StringUtils::format("Swordsman/attack_%d.png", i));
    }
    attackAnimation->setDelayPerUnit(0.18f);
    attackAnimation->setLoops(-1);
}


void Swordsman::initAnimate() {
    initIdleAnimation();
    initRunAnimation();
    initAttackAnimation();
}

```


有关此派生类的相关数据类型和函数在头文件的定义
public:
    virtual void idleAnimate() = 0;
    virtual void runAnimate() = 0;
    virtual void attackAnimate() = 0;
    virtual void stiffAnimate() = 0;
protected:
    bool isFaceRight = true;
    bool isMove = false;
    Vec2 speed;

    Animation* runAnimation;
    Animation* idleAnimation;
    Animation* attackAnimation;


```

我也遇到类似的 问题,一切换动作 就报错,烦死了

Animation 没保存把

怎么保存? 成员变量 还用保存么?

attackAnimation ->retain();

啊,谢谢,搞定了,我去研究下这个函数

当时打断点,看指针变空了,感觉像是被自动释放了,还就是这样

楼上各位!很神奇的使用了retain就可以了!感谢!
能不能解释一下?

lz去研究一下cocos的内存管理吧

百度搜retain或者autorelease有一堆

谢谢!明白了!:14: