0

下面的代码在类的 init() 部分编译并运行良好,但是当我尝试为它创建一个单独的方法时,CCSpriteFrame 最终为空,我想了解这次我让自己进入的概念假设=s

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 可能由于某种原因被清空,有什么想法吗?

4

2 回答 2

0

frame == null当指定的图像不存在时。我认为您最好查看plist文件或找到导致错误的帧。

于 2013-04-18T02:13:10.573 回答
0

我在 C++ 环境中使用 cocos2d 的工作不多,但我知道在 Objective-c 中你可以使用数组之前你必须这样做

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

如果您不这样做,它将始终为空。我会假设它与 C++ 相同

于 2013-04-01T21:05:16.617 回答