2

我正在尝试将动画从转换cocos2dcocos2d-x但无济于事。我没有收到任何明显的错误消息,只是它出现在倒数第二行。

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

4 回答 4

4

您正在将单个字符分配给 sprintf 。然后 sprintf 将字符串写入 fn 指向的内存之外的内存,因为它比 1 个字符长。

代替

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

做这个

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);
}
于 2012-06-14T15:01:27.133 回答
4

使用 C++,

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

...

delete []fn;
于 2012-10-29T12:07:10.007 回答
1

你可以这样

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) ) );
于 2013-11-23T11:49:38.793 回答
0

char* fn = 新字符;没有指定分配给 fn 的大小。

你应该这样写

  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 ;
  }
于 2012-11-09T06:56:14.300 回答