Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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();


_character = CCSprite::createWithSpriteFrameName("girlneutral1.png");
_character->setPosition(ccp(winSize.width * 0.1, winSize.height * 0.5));
_batchNode->addChild(_character, 1);
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]));


the function that loads the animations (_charanimation[] is just an array of CCAnimate):

void HelloWorld::createAnimation(int a, CCString animation_name, int frames, float delay)
    CCArray* animframes = CCArray::createWithCapacity(frames);
    char str[100] = {0};
    for(int i = 1; i <= frames; i++)
        sprintf(str, "%s%d.png", animation_name.getCString(), i);
        CCSpriteFrame* frame = _cache->spriteFrameByName( str );
    _charanimation[a] = CCAnimate::create(CCAnimation::createWithSpriteFrames(animframes, delay));

and I get the animation to work (the one I set with runAction()) but if I try to change the animation, for example, when I touch the screen:

void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
    _character->runAction( CCRepeatForever::create(_charanimation[action]));
    char str[100];
    sprintf(str, "Animation: %d", action);


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
share|improve this question
it's just a hunch but I think you should retain each animation created in the last line of HelloWorld::createAnimation. something like _charanimation[a]->retain(). and if that fixes the problem you should release all the animations in HelloWorld::~HelloWorld() to mark they are not needed any more, at least not in this class. – Ali.S Jul 24 '13 at 18:32
That was exactly the problem, someone else answered to me at stackoverflow. What I did was to call retain() and then at the destructor I call release() for every animate in the array. – nosmirck Aug 1 '13 at 16:45
it was not properly organised answer and I was not entirely sure about it being correct, so I just left it as a comment! but since you say it actually helped you, I've posted same thing as an answer to let others later looking at this question get a proper solution! – Ali.S Aug 1 '13 at 19:31
up vote 2 down vote accepted

You should retain each animation created in the last line of HelloWorld::createAnimation(). something like _charanimation[a]->retain(). And later when this class is getting destroyed (in HelloWorld::~HelloWorld() or whenever you don't need animations any more) you should release all those classes you retained the animations to mark they are not needed any more. Note that you should not delete anything, if those animations where needed in some other class, deleting them will result in that other class malfunctioning. And if it's not needed anywhere else, releasing an instance of CCObject (grand parent of all cocos2dx objects) will automatically delete that instance which means that object will be deleted twice that will result in access violation.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.