I’m having trouble changing animations.
I have a class that represents a character in my game, it subclasses from CCNode. It contains an animNode (CCSpriteBatchNode) which contains a sprite (CCSprite).
The code from my init() function that relates to the heirarchy:
// Get something to give to the anim node.
CCAnimation *anim = this->info->getStandAnimation();
CC_ASSERT(anim);
CCMutableArray *frames = anim->getFrames();
CC_ASSERT(frames);
CC_ASSERT(frames->count() > 0);
CCSpriteFrame *fr = frames->getObjectAtIndex(0);
CC_ASSERT(fr);
CCRect const & rect = fr->getRect();
CC_ASSERT( !CCSize::CCSizeEqualToSize(rect.size, CCSizeZero) );
this->setContentSize(rect.size);
this->animNode = CCSpriteBatchNode::batchNodeWithTexture( fr->getTexture() );
CC_ASSERT(this->animNode);
this->animNode->retain();
this->addChild(this->animNode);
this->sprite = new CCSprite();
CC_ASSERT(this->sprite);
success = this->sprite->initWithBatchNode(
this->animNode,
rect);
CC_ASSERT(success);
this->sprite->setAnchorPoint(CCPointZero);
this->animNode->addChild(this->sprite);
When my game start, it plays the stand animation, which works fine, but when I switch to my move animation I get
Assertion failed: (! m_bUsesBatchNode), function setTexture, file libs/cocos2dx/sprite_nodes/CCSprite.cpp, line 1090.
line 1090 for reference:
CCAssert(! m_bUsesBatchNode, “setTexture doesn’t work when the sprite is rendered using a CCSpriteSheet”);
Call stack:
CCSprite::setTexture(CCTexture2D*) at CCSprite.cpp:1090
CCSprite::setDisplayFrame(CCSpriteFrame*) at CCSprite.cpp:1026
CCAnimate::update(float) at CCActionInterval.cpp:2189
...
My “animation” code:
void CTFTeamMember::runAnimation(CCAnimation *anim, char const * const textureName, unsigned const times, CCFiniteTimeAction *postAnimAction)
{
CC_ASSERT(anim);
CC_ASSERT(textureName);
CC_ASSERT(this->animNode);
CC_ASSERT(this->sprite);
CCAssert(
(times != 0) || !postAnimAction,
"Can't have a post anim action if looping infinitely.");
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(textureName);
CC_ASSERT(texture);
this->animNode->setTexture(texture);
CCActionInterval *action = CCAnimate::actionWithAnimation(anim, false);
CC_ASSERT(action);
if (times < 1)
{
action = CCRepeatForever::actionWithAction(action);
CC_ASSERT(action);
CC_ASSERT(!postAnimAction);
}
else if (times > 1)
{
action = CCRepeat::actionWithAction(action, times);
CC_ASSERT(action);
}
if (postAnimAction)
{
CC_ASSERT(times > 0);
action = CCSequence::actionOneTwo(
action,
postAnimAction);
CC_ASSERT(action);
}
this->sprite->runAction(action);
this->setAnimationAction(action);
}
What am I doing wrong?