schedule可以實現(xiàn)定時器的功能,就是每隔一段時間做什么事情,schedule的調(diào)用者是節(jié)點,所有的節(jié)點都可以調(diào)用schedule函數(shù),參數(shù)需要傳入一個函數(shù)(schedule_selector一個新的選擇器),在函數(shù)中可以完成碰撞檢測等功能。下面就具體來看看這個函數(shù)的用法吧。




bool HelloWorld::init(){ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //schedule傳入一個參數(shù)的時候每一幀都會調(diào)用show函數(shù) //this->schedule(schedule_selector(HelloWorld::show)); //以下的schedule方法中,傳入的第二個參數(shù)是時間,代表多長時間調(diào)用一次show函數(shù) //this->schedule(schedule_selector(HelloWorld::show),1.0); //schedule方法中的前倆個參數(shù)和上邊的相同,第三個參數(shù)是方法調(diào)用的重復(fù)次數(shù),重復(fù)倆次加剛開始的一次 //總共調(diào)用了三次,3.0代表執(zhí)行下邊的語句后多長時間開始調(diào)用函數(shù)show,就是delay的時間 //this->schedule(schedule_selector(HelloWorld::show),1.0,2,3.0); //scheduleUpdate每隔一幀都會調(diào)用update方法,需要我們聲明一下update方法 this->scheduleUpdate(); bRet = true; } while (0); return bRet;}void HelloWorld::update(float dt){ static int i = 0; if(i == 100) { //下次不再調(diào)用update方法,但是CCLog函數(shù)還是會執(zhí)行的。 //this->unscheduleUpdate(); //以下函數(shù)實現(xiàn)相同的功能,它會將這個層的所以schedule方法都停止調(diào)用 this->unscheduleAllSelectors(); } CCLog("i = %d",++i);}//show函數(shù)必須含有一個float類型的參數(shù)void HelloWorld::show(float dt){ static int i = 0; CCLog("time = %d",++i); if(i == 10) { //unschedule停止傳入的參數(shù)代表的方法調(diào)用 //以下代碼不一定需要寫在這個show方法中 this->unschedule(schedule_selector(HelloWorld::show)); }}新聞熱點
疑難解答
圖片精選