4

你好,我正在使用这段代码为使用 cocos2dx 的精灵制作动画。

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("suhas.plist");
    CCLog("#########################");
    CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::batchNodeWithFile("suhas.PNG");
    CCLog("*********************************");
    this->addChild(spriteSheet);
    CCLog("*********************************");
    CCArray *bearArray = new CCArray();
     for(int i = 1; i <= 8; i++)
     {
         CCLog("*********************************");
        char name[32] = {0}; 
         sprintf(name, "bear%d.png",i);
        bearArray->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name));
}
    CCAnimation *walkAnim = CCAnimation::animationWithSpriteFrames(bearArray, 0.1f);
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCSprite *bear = CCSprite::spriteWithSpriteFrameName("bear1.png");
    bear->setPosition(ccp(size.width/2, size.height/2));
    CCAction *walkAction = CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(walkAnim));
    bear->runAction(walkAction);
    spriteSheet->addChild(bear);
    return true;

但是我的代码崩溃了。并给出错误

cocos2d: CCSpriteFrameCache: Trying to use file D:\cocos2dnew\SPRITEAnimDemo\Resources\suhas.png as texture
First-chance exception at 0x00000000 in SPRITEAnimDemo.win32.exe: 0xC0000005: Access violation.
Unhandled exception at 0x00000000 in SPRITEAnimDemo.win32.exe: 0xC0000005: Access violation.
The program '[1012] SPRITEAnimDemo.win32.exe: Native' has exited with code -1073741819 (0xc0000005).

我尝试在谷歌上解决这个问题。请帮我。

4

2 回答 2

0

更改此行

*spriteSheet = CCSpriteBatchNode::batchNodeWithFile("suhas.PNG");

*spriteSheet = CCSpriteBatchNode::batchNodeWithFile("suhas.png");
于 2012-12-21T12:22:51.507 回答
0

在较新版本的 cocos2d-x 中,您可以先添加 batchnode :

 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprite_sheet.plist");
_gameBatchNode = SpriteBatchNode::create("sprite_sheet.png");
this->addChild(_gameBatchNode, 1);

对于动画:

 auto animation = Animation::create();
int i;
for(i = 1; i <= 10; i++) {
    auto name = String::createWithFormat("boom%i.png", i);
    //auto name = String::createWithFormat("meteor-%i.png", i);
    auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(name->getCString());
    animation->addSpriteFrame(frame);
}

animation->setDelayPerUnit(1 / 10.0f);
animation->setRestoreOriginalFrame(true);

请注意,boom.png 和 meteor.png 在 sprite_sheet.plist 内

于 2018-02-15T06:14:44.570 回答