Analysis of Cocos2d-x 2.x to 3.x event listening design change one of the reasons

Source: Internet
Author: User

Dionysoslai 2015/2/28

For the design of the event interception, Cocos2d-x from 2.x to 3.x has undergone a fundamental change, has always been to this, only the simple use of thinking about how to build their own game code, not the design of the pros and cons to explore. Just a while ago in a new game, about 2.x touch event found a design is not humanized problem, this want to COCOS2DX official website response, but Test 3.x, did not find this problem. In this paper, this article describes the problem, analysis of the differences between the design, but also hope to initiate.
A brief description of the event listener differences between 2.x and 3.x:
In 2.x, you need to register an event listener.
In 3.x, you need to create an object of a listener, define a callback method, and finally bind the listener and the event dispatcher.
Note that 2.x is registering an event listener, and in 3.x it is creating a listener object. What difference does it have? Here in 2.x and 3.x write a test demo, you can see a serious design bug problem in 2.x.

The 2.x code is as follows:
. h DelegateTest ();    Virtual ~delegatetest (); VirtualvoidKeybackclicked (); . cppdelegatetest::D elegatetest () {}.cppdelegatetest:: ~delegatetest () {Cclog ("DelegateTest release!!");} booldelegatetest:: Init () {if(!Cclayer:: Init ()) {return false; } This->setkeypadenabled (true);Ccdirector:: Shareddirector ()Gettouchdispatcher ()Addtargeteddelegate ( This,0,true);return true;}void delegatetest:: Keybackclicked () {ccscene* scenegame =delegatetest:: Scene ();Ccdirector:: Shareddirector ()Replacescene (Scenegame); Cclog ("keybackclicked!!!");}

Here is a simple touch Listen Event event demo, which adds a register touch event in Init (), while enabling the function to return keys while pressing the back button to toggle the interface.
The first theoretical analysis of debugging information should be as follows:
Press the back key:
keybackclicked!!!
delegatetest release!!
Press the back key again:
keybackclicked!!!
delegatetest release!!
......
event, the debug information is as follows:
Press the back key:
keybackclicked!!!
Press the back key again:
keybackclicked!!!
Exit the game:
DelegateTest release!!
delegatetest release!!
See the difference! What is the result of this phenomenon? Is that the current scene is not removed, the same can receive touch, when more than one scene appears, its logic will appear disorder!!!
Now the code is ported to 3.x.

The 3.x code is as follows:
. hdelegatetest(); Virtual ~delegatetest(); virtual void onkeyreleased (cocos2d:: Eventkeyboard:: KeyCodeKeyCode,cocos2d:: event*event) override;cocos2d:: eventlistenerkeyboard*M_keyboardlistener;//Keyboard Listenercocos2d:: eventlistenertouchonebyone*M_touchlistener;//Touch Listener.cppdelegatetest::D elegatetest(){}delegatetest::~delegatetest() {log ("DelegateTest release!!");} booldelegatetest::Init () {if(!Cclayer::Init ()) {return false; } M_keyboardlistener =Eventlistenerkeyboard::Create ();    M_keyboardlistener->retain (); m_keyboardlistener->onkeyreleased =cc_callback_2(delegatetest::Onkeyreleased, this); Geteventdispatcher ()->addeventlistenerwithscenegraphpriority (M_keyboardlistener, this);//setKeypadenabled (true); ----no longer applicable M_touchlistener =Eventlistenertouchonebyone::Create ();    M_touchlistener->retain (); M_touchlistener->ontouchbegan =cc_callback_2(delegatetest::Ontouchbegan, this); M_touchlistener->ontouchbegan = [&] (cocos2d:: touch*Touchcocos2d:: event*Event) {return true;    }; Geteventdispatcher ()->addeventlistenerwithscenegraphpriority (M_touchlistener, this);return true;} voiddelegatetest::Onkeyreleased (cocos2d:: Eventkeyboard:: KeyCodeKeyCode,cocos2d:: event*Event) {Scene* Scenelayer =delegatetest::Scene ();Director::GetInstance ()->replacescene (Scenelayer); Log"onkeyreleased!!!");}

The

function is exactly the same as described earlier. The debugging information is as follows:
onkeyreleased!!!
delegatetest release!!
onkeyreleased!!!
delegatetest release!! The
is consistent with the results we want. Why do these two different situations arise? This is because in 2.x, is the registration listener event, so we must correspond to the listener event to unregister. 3.x is the creation of listener objects, so that when the destruction of the object is automatically removed, from the design, 3.x is more easy than 2.x, and is not prone to error.
So how do you change the 2.x so that the results are consistent with our theory? Change the function keybackclicked () code here:

void  Span class= "Hljs-attribute" >delegatetest :: keybackclicked () {ccscene* scenegame =    delegatetest :: Scene (); ccdirector :: Shareddirector     () ->  replacescene (Scenegame); ccdirector :: Shareddirector  () ->  gettouchdispatcher    ->  removedelegate (this ); Cclog ( "keybackclicked!!!" );}  

Of course, in theory 3.x exits the scene, but also to remove the listener object, the function is release (), here is the province. Perhaps people will say, not just add a sentence in 2.x, and forget to write off, itself is their own problem. The first thing to say here is that when the layer is relatively long, it is not a simple matter to write off the touch, while exiting the scene may be called in a subclass, or in another layer, requiring that all layers in the current scene be closed, such as pausing the interface. Another face, when used to Popscene and Pushscene, this is even more a disaster!!! For a game engine, a good design should be able to proactively help developers avoid pits. This is also why when rendering in Cocos2d-x, why not open a thread specifically to implement the logic and display separately. Because this is too difficult, will lead to a lot of detail problems, virtually increase the difficulty of developers, of course, with the development of the engine later, I believe will solve the problem. Detailed can refer to Wang Zhe answer: http://www.zhihu.com/question/27612727/answer/38078748.
For the above code details, you can access the "Delegate Test" content in the author Githup:https://github.com/dionysoslai/cocosamples. 2015 first article, New Year omen, I wish you a better New Year!!! -ps: The first time to use the Markdown editor, feel the Moe!!!

Analysis of Cocos2d-x 2.x to 3.x event listening design change one of the reasons

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.