下面的代碼在類的init()部分編譯和運行良好,但是當我嘗試爲它創建一個單獨的方法時,CCSpriteFrame結束爲null,我想了解我得到的概念性假設這次= S爲什麼這個CCSpriteFrame爲空?
void SceneView::runZoo(Animal& animal, std::string attack) {
//
// Animation using Sprite BatchNode
//
std::string spriteName;
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
CCSize iSize = CCDirector::sharedDirector()->getWinSize();
spriteName = "killerRabbit.plist";
cache->addSpriteFramesWithFile(spriteName.c_str());
spriteName = "killerRabbit1.png";
sprite = CCSprite::createWithSpriteFrameName(spriteName.c_str());
sprite->setPosition(ccp(iSize.width/2 - 160, iSize.height/2 - 40));
spriteName = "killerRabbit.png";
CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create(spriteName.c_str());
spritebatch->addChild(sprite);
this->addChild(spritebatch);
CCArray* animFrames = CCArray::createWithCapacity(15);
spriteName = "killerRabbit";
char str[100] = {0};
for(int i = 1; i <= 9; i++) {
sprintf(str, (spriteName + "%d.png").c_str(), i);
CCSpriteFrame* frame = cache->spriteFrameByName(str);
//Null here
animFrames->addObject(frame);
//Null here
}
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.15f);
sprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));
}
實際的錯誤:
/** Appends an object. Behavior undefined if array doesn't have enough capacity. */
void ccArrayAppendObject(ccArray *arr, CCObject* object)
{
CCAssert(object != NULL, "Invalid parameter!");
object->retain();
arr->arr[arr->num] = object;
arr->num++;
}
這意味着CCSpriteFrameCache可能被清零出於某種原因,任何想法?
@drescherjm 是的,我刪除了一些檢查,因此它可能非正常失敗,我不知道是什麼導致歸零放在首位= S – Hobbyist