Use Promise/Deffered mode to optimize game animations in coco2d html5!

Source: Internet
Author: User

Problem:


When you use cocos2d html to write a series of combat animations, you often encounter the following issue:


// Action 1actionSetp1: function (){... var move = cc. moveTo. create (1, cc. p (100,100); // After moving, execute the actionStep2 function var call = cc. callFunc. create (this. onSetp2, this); var seq = cc. sequence. create ([move, call]); this. _ sprite1.runAction (seq);} // action 2actionSetp1: function (){.... var scale = cc. scaleTo. create (1, 1.5); // execute after moving, actionStep3 function var call = cc. callFunc. create (this. onSetp3, this); var seq = cc. sequence. create ([scale, call]); this. _ sprite2.runAction (seq);} // action 3actionSetp3: function (){.... // no subsequent action, no cc. callFuncvar move = cc. moveTo. create (1, cc. p (100,100); this. _ sprite. runAction (seq );}






Using cc. CallFunc can solve this problem well. However, when there are more than 7 action sequences, it may feel a little powerless,
Every action function needs to know its next action, which undoubtedly increases the coupling between codes.


Solution:
In the past two days and weeks, I did not take a look at the book "go deep into node. js". I talked about how to solve the problem of asynchronous programming in node using the Promise/Deffered mode to solve the problem of deep nesting of asynchronous functions.
I suddenly thought about the problem of game animation in cocos2d html. So I used Deffer to refactor the Code (the following code uses the q. js library to install npm install q). The effect is good:


PlayAction: function () {// The current this object needs to be passed in // The action is executed in the order of actionSetp1, actionSetp2, and actionSetp3. actionSetp1 (this ). then (this. actionSetp2 ). then (this. actionSetp3);} // action 1actionSetp1: function (self ){... // Use q. js creates the defer object var defer = Q. defer (); var move = cc. moveTo. create (1, cc. p (100,100); var call = cc. callFunc. create (function () {// execute defer. the resolve () function indicates that the current action has been completed // you do not need to know what the next action function is. resolve (self) ;}, self); var seq = cc. sequence. create ([move, call]); self. _ sprite1.runAction (seq); return defer. promise; // The returned promise is used to form a promise. then call chain} // action 2actionSetp1: function (self ){.... var defer = Q. defer (); // create defervar var scale = cc. scaleTo. create (1, 1.5); var call = cc. callFunc. create (function () {defer. resolve (self) ;}, self); var seq = cc. sequence. create ([scale, call]); self. _ sprite2.runAction (seq); // The returned promise is used to form a promise. return defer. promise;} // action 3actionSetp3: function (self ){.... // same as var defer = Q. defer (); var move = cc. moveTo. create (1, cc. p (100,100); var call = cc. callFunc. create (function () {defer. resolve (self) ;}, self); self. _ sprite. runAction (seq); // The returned promise is used to form a promise. return defer. promise ;}






The above code runs exactly the same way as the previous version. When you look at it, there are more code, but every action function in the later version does not need to care about the animation chain,
Only focus on the actions you need to complete.


We copy two actions. The action chain is as follows: this. actionSetp1 (this ). then (this. actionSetp2 ). then (this. actionSetp3); // copy. then (this. actionSetp2); // copy. then (this. actionSetp3); let's modify and add a new action. The action chain is as follows: this. actionSetp1 (this ). then (this. actionSetp2 ). then (this. actionSetp3 );. then (this. actionSetp4); // Add. then (this. actionSetp5); // Add






I believe everyone understands the benefits!


---------------------------------------------------------------
Note:
1. q. js is no problem in cocos2d html, but when q. js is used in jsb, it will prompt that setTimeout is not defined. I solve it like this:
mySetTimeout = function(callback, interval) {    var director = cc.Director.getInstance();    director.getScheduler().scheduleCallbackForTarget(director, callback, 0, 1, interval, false);};var setTimeout = setTimeout || mySetTimeout ;




The setTimeout function is simulated using schedule in cocos2d.


2. The context of this function called by then in this. actionSetp1 (this). then (...). then (...) is not your current class object.
So here, each defer. resolve (self); uses self to pass down the current class object.

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.