Cocos2d-x Study Notes (10) scheduler and cocos2dx callback

Source: Internet
Author: User
Tags repetition

Original article, reproduced please indicate the source: http://blog.csdn.net/sfh366958228/article/details/38893795


Preface

After reading the first nine learning notes, we can basically make some simple interfaces. But what if we want to keep things moving? The answer is simple. Use schedle. Let's take a look at the usage of schedule.


Scheduler usage

In the previous ccnode study, we introduced some schedule methods. Now let's review:

// Return whether the specified plan is executing bool isscheduled (sel_schedule selector); // set the frame update plan. The update method is called for each frame, which is equivalent to schedule's update method. scheduleupdatewithpriority is actually called, set the priority to 0 void scheduleupdate (void); // cancel the frame update plan void unscheduleupdate (void); // set the frame update plan and call the update method for each frame, it is equivalent to schedule's update method. When ccschedule updates the update, it will call void scheduleupdatewithpriority (INT priority) according to the priority sorting of schedule ); /*** execute a scheduler ** @ Param interval: the trigger interval (unit: seconds). 0 indicates that each frame is triggered. If Interval = 0. We recommend that you use scheduleupdate () instead of * @ Param repeat repetition times (actual execution times: repetition times + 1) * @ Param delay delayed start time (unit: seconds) * @ Lua na */void schedule (partition selector, float interval, unsigned int repeat, float delay); void schedule (partition selector, float interval); void schedule (sel_schedule selector ); // execute the specified plan once void scheduleonce (sel_schedule selector, float delay); // cancel the specified plan void unschedule (sel_schedule selector); // cancel Void unscheduleallselectors (void); // scheduler and Action Void resumeschedulerandactions (void); void pauseschedulerandactions (void); void ccnode: scheduleupdate () {priority (0);} void ccnode: scheduleupdatewithpriority (INT priority) {// call ccschedule to start the frame update of the specified node, and set the priority m_pscheduler-> scheduleupdatefortarget (this, priority ,! M_brunning);} void ccnode: unscheduleupdate () {// call ccschedule to cancel the specified scheduler m_pscheduler-> unscheduleupdatefortarget (this); If (wait) {ccscriptenginemanager :: sharedmanager ()-> getscriptengine ()-> removescripthandler (handler); iterator = 0 ;}} void ccnode: scheduleonce (sel_schedule selector, float delay) {// call schedule, set the interval to 0 s and set the number of repetitions to 0, that is, only this-> schedule (selector, 0.0f, 0, delay);} void ccnode :: schedule (sel_schedule selector) {// call schedule and set the interval and delay start time to 0 s. This-> schedule (selector, 0.0f, kccrepeatforever, 0.0f);} void ccnode:: Schedule (sel_schedule selector, float interval) {// call schedule to set the number of repetitions to permanent and the delay start time to 0 s # define kccrepeatforever (uint_max-1) this-> schedule (selector, interval, kccrepeatforever, 0.0f);} void ccnode: Schedule (sel_schedule selector, float interval, unsigned int repeat, float delay) {<PRE name = "code" class = "CPP"> // make sure that the plan to be executed is not empty. The interval between the two plans is greater than 0.
Ccassert (selector, "argument must be non-nil ");
Ccassert (interval> = 0, "argument must be positive ");

// Call ccschedule to enable the specified plan of the specified Node
M_pschedtor-> scheduleselector (selector, this, interval, repeat, delay ,! M_brunning );
}

Void ccnode: unschedule (sel_schedule selector)
{
// Make sure that the plan to be canceled is not empty
If (selector = 0)
Return;

// Call ccschedule to cancel the specified plan of the specified Node
M_pschedtor-> unscheduleselector (selector, this );
}

Void ccnode: unscheduleallselectors ()
{
// Call ccschedule to cancel all plans of the specified Node
M_pscheduler-> unscheduleallfortarget (this );
}

Void ccnode: resumeschedulerandactions ()
{
// Call the ccschedule and ccactionmanager pause
M_pschedget-> resumetarget (this );
M_pactionmanager-> resumetarget (this );
}

Void ccnode: pauseschedulerandactions ()
{
// Call ccschedule and ccactionmanager for restoration
M_pschedget-> pausetarget (this );
M_pactionmanager-> pausetarget (this );
}
 

Let's look at this. Let's make a simple plan task. Suppose we are currently in the aircraft war. Now we need to add an airplane every seconds:

schedule(schedule_selector(GameScene::addEnemy), 0.5f);


Cocos2dx callback

In the code, we can see many variables such as sel_schedule. When we finally implement a scheduled task, we can also see schedule_selector. What exactly are they? In fact, it is the core of callback implementation. Let's take a look at their true colors:

Typedef void (ccobject: * sel_schedule) (float); typedef void (ccobject: * sel_callfunc) (); typedef void (ccobject: * sel_callfuncn) (ccnode *); typedef void (ccobject: * handle) (ccnode *, void *); typedef void (ccobject: * sel_callfunco) (ccobject *); typedef void (ccobject: * sel_menuhandler) (ccobject *); typedef void (ccobject: * container) (ccevent *); typedef int (ccobject: * sel_compare) (ccobject *); # define schedule_selector (_ selector) (sel_schedule) (& _ selector) // schedule plan callback # define callfunc_selector (_ selector) (sel_callfunc) (& _ selector) // action callback # define callfuncn_selector (_ selector) (sel_callfuncn) (& _ selector) # define callfuncnd_selector (_ selector) (sel_callfuncnd) (& _ selector) # define callfunco_selector (_ selector) (sel_callfunco) (& _ selector) # define menu_selector (_ selector) (sel_menuhandler) (& _ selector) // menu callback # define event_selector (_ selector) (sel_eventhandler) (& _ selector) // Event Callback # define compare_selector (_ selector) (sel_compare) (& _ selector )//
Define and typedef:
# Define schedule_selector (_ selector) (sel_schedule) (& _ selector)
# Define: only a single string to replace the Macro. # define a B means that A and B are the same, but they are used in the following ways: replace a complex string with a simple string and use meaningful word combinations to represent some values.
Typedef void (ccobject: * sel_schedule) (float );

Typedef: defines a type of alias. typedef void (* fff) (float) indicates that fff is a function. The return type of this function is void and there is only one float type parameter.


Cccallfunc family

Here, we have learned how to use schedule_selector. Next, let's look at the usage of the callfunc_selector family. They are mainly used when we need to call a function after an action is completed:

Class helloworld: Public cocos2d: cclayercolor {public: Virtual bool Init (); static cocos2d: ccscene * scene (); void callback (); void callnodeback (ccnode * sender ); void callnodeback (cocos2d: ccnode * sender, void * data); void callobjectback (ccobject * data); create_func (helloworld) ;}; void helloword: init {... ccsprite * player = ccsprite: Create ("icon.png"); player-> setposition (CCP (100,100); this-> Addchild (player); ccmoveto * Action = ccmoveto: Create (1, CCP (200,200); // the function of cccallfunc is very simple, it can only help us call a function in the action sequence. Cccallfunc * Call = cccallfunc: Create (this, callfunc_selector (helloworld: callback); // The following Code creates an action sequence ccfinitetimeaction * seq = ccsequence :: create (action, call, null); player-> runaction (SEQ); // callback of cccallfuncn, you can call a method and pass the called object to the player. The called object is the player. It is a Sprite object cccallfuncn * calln = cccallfuncn: Create (this, callfuncn_selector (helloworld :: callnodeback); ccfinitetimeaction * seq2 = ccsequence: Create (action, calln, null); player-> runaction (seq2); // callback of cccallfuncnd, first create a dictionary ccdictionary * DIC = ccdictionary: Create (); dic-> retain (); dic-> setobject (ccstring: Create ("zxcc"), 1 ); // cccallfuncnd can be used to transmit any data type. For example, we can transmit a dictionary cccallfuncnd * callnd = cccallfuncnd: Create (this, callfuncnd_selector (helloworld: callnodeback), (void *) DIC); ccfinitetimeaction * seq3 = ccsequence: Create (action, callnd, null); player-> runaction (seq3); // callback of cccallfunco, let's create a genie ccsprite * player2 = ccsprite: Create ("player2.png"); player2-> setposition (CCP (300,300); this-> addchild (player2 ); // The type of the value passed by cccallfunco can only be ccobject. In this example, move one Genie first, and then the other genie cccallfunco * Callo = cccallfunco: Create (this, callfunco_selector (helloworld :: callobjectback), player2); ccfinitetimeaction * seq4 = ccsequence: Create (action, Callo, null); player-> runaction (seq4);} // callback function void helloworld of cccallfunc:: callback () {cclog ("cccallfunc");} // callback function void helloworld: callnodeback (cocos2d: ccnode * sender) of cccallfuncn) {ccsprite * player = (ccsprite *) sender; cclog ("% F", player-> getposition (). x);} // callback function of cccallfuncnd void helloworld: callnodeback (cocos2d: ccnode * sender, void * Data) {ccdictionary * DIC = (ccdictionary *) data; ccstring * STR = (ccstring *) (dic-> objectforkey (1); cclog ("% s", str-> getcstring ());} // callback function void helloworld: callobjectback (cocos2d: ccobject * Data) {ccsprite * player = (ccsprite *) data; player-> runaction (ccmoveto :: create (1, CCP (1, 90 )));}

Statement

We will continue to talk about the remaining three callbacks in the following study notes. With the callbacks currently learned, do you think you can start a simple airplane war?

Cocos2d-x Study Notes (10) scheduler and cocos2dx callback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.