在Unity中有OnGUI、Update等每一帧都调用的函数,我们把一些逻辑以及界面更新写在这些函数里面。
顺手在Cocos2d-x中就Virtual void Update(float dt),然后实现,但是断点却没有进去!
class CocosRakNet :public TristanaLayer{public: static CCScene* scene(); virtual bool init(); void update(float delta);
void CocosRakNet::update(float delta){ int i=0;}
原来在Cocos2d-x中的每一帧执行事件,只有Update是不行的,还要在init()函数里面加上scheduleUpdate(),这样才会每一帧都调用update()。
bool CocosRakNet::init(){ bool bRet=false; do { CC_BREAK_IF(!TristanaLayer::init()); CCSize winSize=CCDirector::sharedDirector()->getWinSize(); CCMenu* menu=CCMenu::create(); CCMenuItemImage* pMenuItemImage=CCMenuItemImage::create("CloseNormal.png","CloseSelected.png","",this,menu_selector(CocosRakNet::SendMessageCallBack)); pMenuItemImage->setPosition(ccp(winSize.width/2,winSize.height/2)); menu->addChild(pMenuItemImage); CCMenuItemImage* pRequestRoleInfoMenuItemImage=CCMenuItemImage::create("CloseNormal.png","CloseSelected.png","",this,menu_selector(CocosRakNet::RequestRoleInfo)); pRequestRoleInfoMenuItemImage->setPosition(ccp(200,200)); menu->addChild(pRequestRoleInfoMenuItemImage); menu->setPosition(CCPointZero); addChild(menu); //更新函数 scheduleUpdate(); bRet=true; } while (0); return bRet;}
那么 schedule() 这个函数呢?。
Schedule() 函数有两种方式,一种带时间参数,一种不带时间参数。
带时间参数的,间隔指定时间,然后执行指定的函数,达到定时刷新的效果。
不带时间参数的,每一帧刷新,执行指定的函数,作用效果就和scheduleUpdate() 函数差不多效果,不过函数可以自己指定。
不带时间参数的写法(写在init()中),这样每一帧执行update() 函数
schedule(schedule_selector(CocosRakNet::update));
带时间参数的写法,每隔指定时间执行update() 函数
schedule(schedule_selector(CocosRakNet::update),3.0f);
注意这里的时间间隔是 秒 为单位
来看schedule()函数的参数解释:
void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
/** * Schedules a custom selector. * * If the selector is already scheduled, then the interval parameter will be updated without scheduling it again. * @code * // firstly, implement a schedule function * void MyNode::TickMe(float dt); * // wrap this function into a selector via schedule_selector marco. * this->schedule(schedule_selector(MyNode::TickMe), 0, 0, 0); * @endcode * * @param interval Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead. * @param repeat The selector will be excuted (repeat + 1) times, you can use kCCRepeatForever for tick infinitely. * @param delay The amount of time that the first tick will wait before execution. */
这个函数用来执行一个计划任务。PS:百度的翻译。。
interval:如果这个参数是0,那么每一帧都会执行指定函数,这样的话,建议使用scheduleUpdate() 函数
repeat:指定函数将会执行指定次数。
delay:函数执行前,将会有delay时间的延迟。
如何让只执行一次函数?
this->scheduleOnce(schedule_selector(HelloWorld::Update), 5.0f);
这样就会延迟5秒之后执行一次Update,而且只执行一次!
那如何停止schedule ?
1.停止scheduleUpdate()关联的update()函数
unscheduleUpdate();
2.停止schedule() 关联的自己的函数
schedule(schedule_selector(CocosRakNet::update),3.0f,10,5.0f); unschedule(schedule_selector(CocosRakNet::update));
3. 停止所有的schedule,不管是自己的还是默认关联的
//更新函数 scheduleUpdate(); schedule(schedule_selector(CocosRakNet::update)); schedule(schedule_selector(CocosRakNet::update),3.0f); unschedule(schedule_selector(CocosRakNet::update)); unscheduleAllSelectors();