C++ 为什么此CCSpriteFrame为空?
下面的代码在类的init()部分编译并运行良好,但是当我尝试为它创建一个单独的方法时,CCSpriteFrame最终为空,我想了解一下我在这个time=s中的概念假设C++ 为什么此CCSpriteFrame为空?,c++,cocos2d-iphone,C++,Cocos2d Iphone,下面的代码在类的init()部分编译并运行良好,但是当我尝试为它创建一个单独的方法时,CCSpriteFrame最终为空,我想了解一下我在这个time=s中的概念假设 void SceneView::runZoo(Animal& animal, std::string attack) { // // Animation using Sprite BatchNode // std::string spriteName; CCSpriteFrameCa
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)) );
}
这意味着CpSpReTeFrEcCurCH可能由于某种原因而被取消,有任何想法吗?
< P>我在C++环境中没有用COCOS2D工作过多,但我知道在Objtovi-C中,在使用数组之前,必须做这个< /P>CCArray *array = [[CCArray alloc] initWithCapacity:15];
如果不这样做,它将始终为空。当指定的图像不存在时,我假设C++
frame==null
也是如此。我想你最好查看一下plist文件,或者找出是哪个帧导致了错误。@drescherjm是的,我删除了一些检查,所以它可能会失败,我不确定是什么原因导致了零位=sOh,是不是CCArray*animFrames=CCArray::createWithCapacity(15)有什么问题代码>?它在init()
method=saf中工作。在浏览CPP文档之后,请尝试以下操作:CCArray*animFrames=CCArray::arrayWithCapacity(15);我也刚刚检查过,他们用createWithSomething()
CCArray *array = [[CCArray alloc] initWithCapacity:15];