Cocos2d-x Timers (Automatic timers and custom timers)
HelloWorldScene.h
1 #ifndef __helloworld_scene_h__2 #define__helloworld_scene_h__3 4#include"cocos2d.h"5 6 classHelloWorld: PublicCocos2d::layer7 {8 9 Private:TenCocos2d::labelttf *label;//define a label to display the moved picture One A - Public: - //there ' s no ' id ' in CPP, so we recommend returning the class instance pointer the Staticcocos2d::scene*Createscene (); - - //Here ' s a difference. Method ' init ' in cocos2d-x returns BOOL, instead of returning ' ID ' in Cocos2d-iphone - Virtual BOOLinit (); + - //implement the "Static Create ()" Method manually + Create_func (HelloWorld); A at //Override the Update method (the interval of time, which is the interval between two frames), and he executes it once at each frame . - Virtual voidUpdatefloatDT); - - //custom methods for changing the time interval - Virtual voidTimerhandler (floatDT); - }; in - #endif //__helloworld_scene_h__
HelloWorldScene.cpp
1#include"HelloWorldScene.h"2#include"cocostudio/cocostudio.h"3#include"ui/cocosgui.h"4 5 using_ns_cc;6 7 using namespaceCocostudio::timeline;8 9scene*Helloworld::createscene ()Ten { One //' Scene ' is an Autorelease object AAuto scene =scene::create (); - - //' Layer ' is an Autorelease object theAuto Layer =helloworld::create (); - - //add layer as a child to scene -Scene->addChild (layer); + - //return the scene + returnscene; A } at - //On "Init" need to initialize your instance - BOOLHelloworld::init () - { - ////////////////////////////// - //1. Super init First in if( !layer::init ()) - { to return false; + } - theLabel = Labelttf::create ("Bobo","Courier", -); * addChild (label); $ Panax Notoginseng - //you can set the time interval to automatically convert a method to a pointer type by using the Schedule_selector () method theSchedule (Schedule_selector (Helloworld::timerhandler),1); + A //time interval cannot be adjusted the //scheduleupdate ();//start the update and let it perform the move + - $ return true; $ } - - //implement a custom method that can change the time interval the voidHelloworld::timerhandler (floatDT) { - WuyiLog">>>>>>>>>>>>>"); the //set Move Label object, move the upper right corner of each frame (1, 1) -Label->setposition (label->getposition () + Point (Ten,Ten)); Wu - //ways to stop moving About if(Label->getpositionx () > -) { $Unscheduleupdate ();//Stop Moving - } - - A } + the - //implement the Update method and constantly move the label $ voidHelloworld::update (floatDT) { the the //set Move Label object, move the upper right corner of each frame (1, 1) theLabel->setposition (label->getposition () + Point (1,1)); the - //ways to stop moving in if(Label->getpositionx () > -) { theUnscheduleupdate ();//Stop Moving the } About the}
Cocos2d-x Timers (Automatic timers and custom timers)