你好我正在使用这段代码使用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).
我试着在google上解决这个问题。请帮帮我。
答案 0 :(得分:0)
更改此行
*spriteSheet = CCSpriteBatchNode::batchNodeWithFile("suhas.PNG");
与
*spriteSheet = CCSpriteBatchNode::batchNodeWithFile("suhas.png");
答案 1 :(得分: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&amp; meteor.png在sprite_sheet.plist
中