How to set multiple animations in cocos2d-x

I’m currently starting with cocos2d-x to build games for blackberry/android/iOS.

I have the png and plist for the animations of a character created with texturepacker. I load them with `CCSpriteBatchNode` and `CCSpriteFrameCache` then I use a function created by me that loads all frames into an array of frames, then create a `CCAnimation` object and store the `CCAnimate` object created with the animation (code is more clear) the thing is that I have a function that detect touches and it is supposed to cycle through all animations, but it always crashes.

here is some code (this goes in the `init()`):

    batchNode = CCSpriteBatchNode::create("Character/girl.png");
    _cache = CCSpriteFrameCache::sharedSpriteFrameCache();

    _cache->addSpriteFramesWithFile("Personajes/girl.plist");

    _character = CCSprite::createWithSpriteFrameName("girlneutral1.png");
    _character->setPosition(ccp(winSize.width * 0.1, winSize.height * 0.5));
    _batchNode->addChild(_character, 1);
    this->addChild(_batchNode);
    createAnimation(0, "girlpush", 8, 0.15f);
    createAnimation(1, "girlneutral", 4, 0.3f);
    createAnimation(2, "girlcrash", 12, 0.3f);
    createAnimation(3, "girljump", 12, 0.2f);
    createAnimation(4, "girltrick", 12, 0.3f);

    _character->runAction(CCRepeatForever::create( _charanimation[3]));

    this->setTouchEnabled(true);

the function that loads the animations (*charanimation = ;
for
{
sprintf, i);
CCSpriteFrame* frame =*cache~~>spriteFrameByName;
animframes~~>addObject(frame);
}
_charanimation[a] = CCAnimate::create(CCAnimation::createWithSpriteFrames(animframes, delay));
//_charanimation[a]->getAnimation()~~>setLoops;
}
</pre>

and I get the animation to work `) but if I try to change the animation, for example, when I touch the screen:
<pre>
void HelloWorld::ccTouchesBegan
{
action++;
action=5;
_character->stopAllActions();
_character->runAction( CCRepeatForever::create(_charanimation[action]));
char str[100];
sprintf(str, “Animation: d”, action);
pLabel~~>setString(str);

}

it crashes… I don’t know if I am doing it wrong, if anyone could help, I will appreciate.

If I change the animation in `runAction()` it shows the animation correctly, but I can’t change ingame with touches.

by the way, this is the error I get in console:

cocos2d-x debug info Assert failed: reference count should greater than 0
    In function retain -- ..\..\cocoa\CCObject.cpp:92 m_uReference > 0 -- assertion failed

I opened this question in stackoverflow and gamedev too.

What is _charanimation? A C-style array? I would suggest using a CCArray instead :). If you want to use your array then you need to make sure you call retain() on each CCAnimate created (and release it on destruction). Otherwise use a CCArray and you only need to retain/release the CCArray and just add the CCAnimate objects to that.

C.J. Kimberlin wrote:

What is charanimation? A C-style array? I would suggest using a CCArray instead :). If you want to use your array then you need to make sure you call retain on each CCAnimate created . Otherwise use a CCArray and you only need to retain/release the CCArray and just add the CCAnimate objects to that.
Yes, it is a C-styke array, also I used a C++ std vector but didn’t work. I tried with CCArray, but failed, when i try
character~~>runAction animframes~~>objectAtIndex(action))); it crashes, but with different error… I don’t know what I’m doing wrong, I can’t figure out how CCArray works.

I tried using retain and worked perfectly, still, I would like to know how to get it to work with CCArray.

You have to retain the CCArray as well. Any CCObject that isn’t (or can’t) be added as a child has to be retained or you lose it at the end of the update tick.