cocos2dx之scheduleUpdate在onEnter被重写的时候

本文详细介绍了Cocos2dx中使用scheduleUpdate方法进行界面更新的正确方式,强调了在重写onEnter方法时必须调用基类的onEnter方法以确保能够正常接收消息。此外还介绍了如何通过回调函数schedule指定执行方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/ganpengjin1/article/details/18257653

cocos2dx之scheduleUpdate在onEnter被重写的时候,onEnter方法一定要添加CCLayer::onEnter(),否则就不能正常更新界面。


在cocos2dx中,我们需要不断地去更新某个动作,我们可以用:

scheduleUpdate,但是这个时候我们需要重写CCObject中的update方法,初始化scheduleUpdate有两种方法:

1,可以在自己的init方法中初始化它。

2,可在onEnter中初始化它,但是一定要CCLayer::onEnter()否则执行不到你的update

当然,你还可以利用回调函数schedule来指定自己的执行方法来代替update。


初始化,那么它就会进入init,这个方法你可以初始化你的layer和一些sprites,如果你想自己重写layer的onEnter/onExit的话,那么务必请注意,你要在你的onEnter/onExit里面写上基类的Base::onEnter、Base::onExit,否则的话,程序会收不到消息的。

举例子说明:

void GameController::InitController() {  

 CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector( GameController::menuCloseCallback));

}



我初始化我的menu,回调一个:HelloWorld::menuCloseCallback

才响应menu的点击消息。

如果我这个时候在GameController里面没有重写onEnter/onExit,那么我就它自动会接受到menu的点击消息。但是,一旦你重写了onEnter/onExit,就一定要写基类的onEnter/onExit的调用,否则,我在初始化menu的时候不会回调到GameController::menuCloseCallback里面。也就是不会接受到消息。


我们看下layer的onEnter/onExit的源码:

  1. void CCLayer::onEnter()  
  2. {  
  3.     CCDirector* pDirector = CCDirector::sharedDirector();  
  4.     // register 'parent' nodes first  
  5.     // since events are propagated in reverse order  
  6.     if (m_bIsTouchEnabled)  
  7.     {  
  8.         this->registerWithTouchDispatcher();  
  9.     }  
  10.   
  11.     // then iterate over all the children  
  12.     CCNode::onEnter();  
  13.   
  14.     // add this layer to concern the Accelerometer Sensor  
  15.     if (m_bIsAccelerometerEnabled)  
  16.     {  
  17.         pDirector->getAccelerometer()->setDelegate(this);  
  18.     }  
  19.   
  20.     // add this layer to concern the kaypad msg  
  21.     if (m_bIsKeypadEnabled)  
  22.     {  
  23.         pDirector->getKeypadDispatcher()->addDelegate(this);  
  24.     }  
  25. }  
  26.   
  27. void CCLayer::onExit()  
  28. {  
  29.     CCDirector* pDirector = CCDirector::sharedDirector();  
  30.     if( m_bIsTouchEnabled )  
  31.     {  
  32.         pDirector->getTouchDispatcher()->removeDelegate(this);  
  33.         unregisterScriptTouchHandler();  
  34.     }  
  35.   
  36.     // remove this layer from the delegates who concern Accelerometer Sensor  
  37.     if (m_bIsAccelerometerEnabled)  
  38.     {  
  39.         pDirector->getAccelerometer()->setDelegate(NULL);  
  40.     }  
  41.   
  42.     // remove this layer from the delegates who concern the kaypad msg  
  43.     if (m_bIsKeypadEnabled)  
  44.     {  
  45.         pDirector->getKeypadDispatcher()->removeDelegate(this);  
  46.     }  
  47.   
  48.     CCNode::onExit();  
  49. }  

看了源码就知道了,如果不调用的基类的onEnter/onExit那么我无法得到消息代理。也就不能接收到消息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值