removeChildByTag、schedule、schedule_selector

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);//清除跟该节点有关的所有资源
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值