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