cocos2d-x -- removeChild

Test5::Test5()
{
    CCSprite* sp1 = CCSprite::create(s_pPathSister1);
    CCSprite* sp2 = CCSprite::create(s_pPathSister2);
    
    sp1->setPosition(ccp(100,160));
    sp2->setPosition(ccp(380,160));

    CCRotateBy* rot = CCRotateBy::create(2, 360);
    CCActionInterval* rot_back = rot->reverse();
    CCAction* forever = CCRepeatForever::create(
                                                    (CCActionInterval*)(CCSequence::create(rot, rot_back, NULL)) 
                                                );
    CCAction* forever2 = (CCAction*)(forever->copy()->autorelease());
    forever->setTag(101);
    forever2->setTag(102);
                                                  
    addChild(sp1, 0, kTagSprite1);
    addChild(sp2, 0, kTagSprite2);
            
    sp1->runAction(forever);
    sp2->runAction(forever2);
    
    schedule( schedule_selector(Test5::addAndRemove), 2.0f);
}

void Test5::addAndRemove(float dt)
{
    CCNode* sp1 = getChildByTag(kTagSprite1);
    CCNode* sp2 = getChildByTag(kTagSprite2);
//这里需要对计数加一,否则removeChild的时候会被释放掉
    sp1->retain();
    sp2->retain();
    //false是表示不删除该节点的动作,true表示删除
    removeChild(sp1, false);
    removeChild(sp2, true);
    //重新加载到层中
    addChild(sp1, 0, kTagSprite1);
    addChild(sp2, 0, kTagSprite2);
    
    sp1->release();
    sp2->release();
}

 

你可能感兴趣的:(cocos2d-x)