2

I'm trying to convert an animation from cocos2d to cocos2d-x but to no avail. I'm not getting any obvious error message, only that it occurs on the second to last line.

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("swim_male.plist"); CCSpriteBatchNode *sceneSpriteBatchNode = CCSpriteBatchNode::batchNodeWithFile("swim_male.png"); this->addChild(sceneSpriteBatchNode); CCAnimation* animation = CCAnimation::animation(); animation->setDelayPerUnit(.05f); char* fn = new char; for (int i = 1; i <= 10; i++) { sprintf(fn, "character_Male00%02d.png", i); CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fn); animation->addSpriteFrame(pFrame); } CCSprite *spriteAnim = CCSprite::spriteWithSpriteFrameName("character_Male0001.png"); spriteAnim->setPosition( ccp(100, 200) ); CCAnimate *animate = CCAnimate::actionWithAnimation(animation); CCAction *act = CCRepeatForever::actionWithAction(animate); spriteAnim->runAction(act); sceneSpriteBatchNode->addChild(spriteAnim, 2); 
7
  • what the text of the error message? Commented Jun 14, 2012 at 14:42
  • it means that one of variables is NULL. check in debugger, what variable is NULL and causes crash Commented Jun 14, 2012 at 14:49
  • There's something wrong with this line: Commented Jun 14, 2012 at 14:50
  • CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fn); even though the variable fn gives correct values Commented Jun 14, 2012 at 14:50
  • if pFrame is NULL, then there is no such sprite frame in spriteframe cache. check console. there can be error message. I assume it will begin with "cocos2d: CCSpriteFrameCache: " Commented Jun 14, 2012 at 14:56

4 Answers 4

4

You are allocating a single character to sprintf into. sprintf is then writing the string into memory outside of that pointed to by fn because it it longer than 1 character.

Instead of

char* fn = new char; for (int i = 1; i <= 10; i++) { sprintf(fn, "character_Male00%02d.png", i); CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fn); animation->addSpriteFrame(pFrame); } 

do this

char fn[128]; for (int i = 1; i <= 10; i++) { sprintf(fn, "character_Male00%02d.png", i); CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fn); animation->addSpriteFrame(pFrame); } 
Sign up to request clarification or add additional context in comments.

Comments

4

using c++,

char* fn = new char --> bad; char* fn = new char[128]; ... delete []fn; 

Comments

1

You can Right like this

CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("horse.png"); CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("horse.plist"); hero = CCSprite::createWithSpriteFrameName("horse_1.png"); addChild(spritebatch); spritebatch->addChild(hero); CCArray* animFrames = CCArray::createWithCapacity(16); char str[100] = {0}; for(int i = 1; i < 16; i++) { sprintf(str, "horse_%i.png", i); CCSpriteFrame* frame = cache->spriteFrameByName( str ); animFrames->addObject(frame); } CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, .1f); animation->setRestoreOriginalFrame(true); hero->runAction( CCRepeatForever::create( CCAnimate::create(animation) ) ); 

Comments

0

char* fn = new char; doesn't specify the allocated size to fn.

you should write like

 for (int i = 1; i <= 10; i++) { char* fn = new char[strlen("character_Male0002d.png")+sizeof(i)+1]; sprintf(fn, "character_Male00%02d.png", i); CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fn); animation->addSpriteFrame(pFrame); delete fn; fn = NULL ; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.