何时在动画结束后释放CCSpriteBatchNode

时间:2014-02-06 08:05:52

标签: ios cocos2d-x

我正在播放动画,并希望在动画完成播放后立即从父级(CCLayer)中删除其批处理节点。我将它作为CCsequence运行,所以我有一个名为 animationFinished 的函数,它在完成播放时被调用。但是我无法在此函数中从父项中删除批处理节点,因为它已被释放并且没有任何其他保留。所以游戏崩溃了,因为引擎仍在使用它。

所以基本上我可以在什么情况下释放我的批处理节点。如果我不这样做,那么它将一次又一次地添加到CClayer。请参阅下面的代码

void GameLayer::playAnimation(const AnimationFileAndTypeInfo &powerFileInfo,float placeAtScreenWidthRatio,float placeAtScreenHeightRatio){

  std::string firstFrame,frameName,plist,plistPng;    

  plist=powerFileInfo.animFileInfo.plist;
    plistPng=powerFileInfo.animFileInfo.plistPng;
    firstFrame=powerFileInfo.animFileInfo.firstFrame;
    frameName=powerFileInfo.animFileInfo.frameName;

    // generic animation play code
    CCSpriteFrameCache *animCache = CCSpriteFrameCache::sharedSpriteFrameCache();
    animCache->addSpriteFramesWithFile(plist.c_str());

    _animPlayBatchNode = CCSpriteBatchNode::create(plistPng.c_str(), 100);
    this->addChild(_animPlayBatchNode,kForeground+1);

    short numberOfFrames=getPlistFramesCount(plist.c_str(),"frames");// plist is the full name with postfix .plist

    CCArray *spriteframes = CCArray::createWithCapacity(numberOfFrames);
    spriteframes->retain();

    _animPlayFirstSprite = CCSprite::createWithSpriteFrameName(firstFrame.c_str());// with postfix .png
    _animPlayBatchNode->addChild(_animPlayFirstSprite,kForeground+1);
    _animPlayFirstSprite->setPosition(ccp(_screenSize.width*placeAtScreenWidthRatio, _screenSize.height*placeAtScreenHeightRatio));

    for(int i = 1; i <= numberOfFrames; i++)
    {
        char szName[50] = {0};
        sprintf(szName, "%s%i.png",frameName.c_str(),i);
        CCSpriteFrame* frame = animCache->spriteFrameByName(szName);
        spriteframes->addObject(frame);
    }

    CCAnimation *animation = CCAnimation::createWithSpriteFrames(spriteframes,0.05f);
    animation->retain();
    animation->setDelayPerUnit(0.08f);// 0.05f

    CCSequence *seq=CCSequence::createWithTwoActions(CCAnimate::create(animation), CCCallFuncO::create(animation,callfuncO_selector(GameLayer::animationFinished),(CCObject*)_animPlayBatchNode));

    _animPlayFirstSprite->runAction(seq);

    spriteframes->release();
    animation->release();

}
void GameLayer::animationFinished(CCObject *sender){

CCSpriteBatchNode *animBatchNode = (CCSpriteBatchNode*) sender;
animBatchNode->removeAllChildrenWithCleanup(true);
_isAnimationAlreadyPlaying=false;

//   animBatchNode->removeFromParent() , crashes if I do this

}

0 个答案:

没有答案