Test4::Test4() { CCSprite *sp1 = CCSprite::create(s_pPathSister1); CCSprite *sp2 = CCSprite::create(s_pPathSister2); sp1->setPosition( ccp(100,160) ); sp2->setPosition( ccp(380,160) ); addChild(sp1, 0, 2); addChild(sp2, 0, 3); //在第二秒的时候执行delay2函数 schedule( schedule_selector(Test4::delay2), 2.0f); //在第四秒的时候执行delay4函数 schedule( schedule_selector(Test4::delay4), 4.0f); } void Test4::delay2(float dt) { CCSprite* node = (CCSprite*)(getChildByTag(2)); CCAction* action1 = CCRotateBy::create(1, 360); node->runAction(action1); } void Test4::delay4(float dt) { unschedule(schedule_selector(Test4::delay4)); removeChildByTag(3, false);//通过tag删除节点 }
void CCNode::removeChildByTag(int tag, bool cleanup) { CCAssert( tag != kCCNodeTagInvalid, "Invalid tag"); CCNode *child = this->getChildByTag(tag); if (child == NULL) { CCLOG("cocos2d: removeChildByTag: child not found!"); } else { this->removeChild(child, cleanup); } }
void CCNode::removeChild(CCNode* child, bool cleanup) { // explicit nil handling if (m_pChildren == NULL) { return; } if ( m_pChildren->containsObject(child) ) { this->detachChild(child,cleanup); } }
void CCNode::detachChild(CCNode *child, bool doCleanup) { // IMPORTANT: // -1st do onExit // -2nd cleanup if (m_bIsRunning) { child->onExitTransitionDidStart(); child->onExit(); } // If you don't do cleanup, the child's actions will not get removed and the // its scheduledSelectors_ dict will not get released! if (doCleanup)//转到这里你会发现,为真的话,则清除该节点及其子节点 { child->cleanup(); } // set parent nil at the end child->setParent(NULL);//为假的话,不清除节点,直接对其父节点赋值空 m_pChildren->removeObject(child);//清除跟该节点有关的所有资源 }