Understanding the variable scope

I have this problem: I was trying to build many animations, right?

In the init() I’ve made something like this:

GameScene::init()
{
    CCLayer::init()
    ......
    more code
    ......

    prepareAnimations()

    this->scheduleUpdate()
}

GameScene::update(float t)
{
    if (timeToTriggerNewAction)
    {
        character->runAction(characterRunningAction ) // I get an error every time I've tried
    }
}

GameScene::prepareAnimations()
{
    spriteCache = CCSpriteFrameCache::sharedSpriteFrameCache();
    spriteCache->addSpriteFramesWithFile("character.plist");
    spriteSheet = CCSpriteBatchNode::create("character.png");

    characterSprite = CCSprite::createWithSpriteFrameName("spriteGeneric.png");
    characterSprite->addChild(spriteSheet);
    characterLayer->addChild(characterSprite);

    CCArray* characterRunningFrames = new CCArray();
    //for() loop adding the frames...
    characterRunningAnimation = CCAnimation::createWithSpriteFrames(characterRunningFrames, 0.08f);
    characterRunningAction = CCAnimate::create(characterRunningAnimation);
    // Here I've tried:
    characterRunningAction->retain();
    characterRunningAnimation->retain(); // And nothing happens.. I get errors too...
}

I don’t know how I could do something like having a list of actions, and then run one of them from the update method.

Thanks a lo in advance!

On GameScene::update() definition of variable “characterRunningAction” is unknown.

Solutions:

  1. make the CCArray* characterRunningFrames global (Not the best idea)
  2. Add CCArray* characterRunningFrames as private/public member on the GameScene class (header file)

P. E. wrote:

On GameScene::update() definition of variable “characterRunningAction” is unknown.
>
Solutions:

  1. make the CCArray* characterRunningFrames global (Not the best idea)
  2. Add CCArray* characterRunningFrames as private/public member on the GameScene class (header file)

I’ve forgot to mention that I’ve tried that, too. I’ve solved this using functions for every animation. I don’t like that approach because I wanted to have the animations loaded. I don’t know what I’m missing here. I’m doing the ->retain() thing, but the game crashes every time I try to run an action from the update()

You are adding the CCSpriteBatchNode as child of the CCSprite instead of the other way around. Maybe that’s the problem?

Maybe you should make sure that your sprite have finish the action when it go to run action next time.

Steven Lee wrote:

Maybe you should make sure that your sprite have finish the action when it go to run action next time.

That’s true: one Action can only be run once at a time. If you are trying to run the same action that is already running, it will cause an error.
You could use numberOfRunningActions() on the Sprite to know if your animation finished, or isDone() on the Action to know if it has finished.

I also usually stop all animations before starting a new one, just in case. You can use stopAllActions() on the Sprite.
You can also categorize and tag your animations and use stopActionByTag().

Steven Lee wrote:

Maybe you should make sure that your sprite have finish the action when it go to run action next time.

Yeah!, it was that… and is funny, because in all my present animation methods I’ve started with “character->stopAllActions()”… But when I was trying the other way, I’ve never noted that…

Thanks!