2013-04-01 20 views
0

下面的代碼在類的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可能被清零出於某種原因,任何想法?

+0

@drescherjm 是的,我刪除了一些檢查,因此它可能非正常失敗,我不知道是什麼導致歸零放在首位= S – Hobbyist

回答

0

我還沒有下過功夫用的cocos2d在C++環境,但我知道,在Objective-C,然後纔可以使用數組,你必須這樣做

CCArray *array = [[CCArray alloc] initWithCapacity:15]; 

如果你不這樣做,這將永遠是空。我會假設它與C++相同

+0

哦,有什麼錯'CCArray * animFrames = CCArray :: createWithCapacity(15);'?在查看CPP文檔後,它在'init()'方法= s – Hobbyist

+0

中工作,請嘗試以下操作: CCArray * animFrames = CCArray :: arrayWithCapacity(15); –

+0

我剛剛檢查過,他們用'createWithSomething()替換了所有不同名稱的方法' – Hobbyist

0

frame == null當指定的圖像不存在時。我認爲你最好通過plist文件或找出哪個幀導致錯誤。