Search code examples
c++cocos2d-iphonecocos2d-x

Cocos2d-x CCSprite->setDisplayFrame AccessViolation crash


So far I could track down every error/crash I had in my cocos2dx application, but now I feel myself dumb. I have an array of CCSprite pointers, and what I want to do is to change it's display frame from the cache when the player has x score.

All the sprite frames are read from a plist file, and the sprite is there and working, because I can see it without any problem before trying to switch to the new frame. The "background.png" is a valid sprite frame name, since I'm using it to create the actual sprite which works.

This line works fine (or at least it seems to work)

CCSpriteFrame* frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("background.png");

The problem is with this line:

for (int c = 0; c < sizeof(mBackground) / sizeof(CCSprite*); c++){
    mBackground[c]->setDisplayFrame(frame);
} 

This one also crashes:

CCSpriteFrame* frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("background.png");
mBackground[0]->setDisplayFrame(frame);

I ran out of ideas, I would really appreciate any help.


Solution

  • Make sure image exists in cache. If not exists then load spriteframe to cache first. Also add mBackground[0] to parent before changing display frame.

    if(mBackground[0] && frame)
    {
        mBackground[0]->setDisplayFrame(frame);
    }