Analysis of cocos2d-x 2. x to 3. x event listening design change one of the reasons, cocos2d-x3.x

Source: Internet
Author: User

Analysis of cocos2d-x 2. x to 3. x event listening design change one of the reasons, cocos2d-x3.x

DionysosLai 2015/2/28

For the design of event listening, the cocos2d-x from 2. x to 3. x has undergone a fundamental change. For a long time, it simply considers how to build its own game code and does not explore the advantages and disadvantages of its two designs. Only when I was playing a new game some time ago. x's touch event found a problem of design humanization. I wanted to respond to the cocos2dx official website, but tested 3. x, this problem was not found. In this regard, this article will elaborate on this issue, analyze the differences between the two designs, and hope to inspire others.
Briefly describe the differences between event listening in 2.x and 3.x:
In 2.x, You need to register an event listener.
In 3.x, you need to create a listener object, define the callback method, and finally bind the listener to the event distributor.
Note that 2. x registers an event listener, and 3. x creates a listener object. What is the difference between the two? Here we write a test demo in 2.x and 3.x respectively, and we can see a serious design bug in 2.x.

2. The x code is as follows:
.h    DelegateTest();    virtual ~DelegateTest();    virtual void keyBackClicked();    .cpp    DelegateTest::DelegateTest(){}.cppDelegateTest::~DelegateTest(){    CCLOG("DelegateTest release!!");}bool DelegateTest::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 listening event demo. Add the registration touch event in init (), enable the return key function, and switch the interface when you press the return key.
The debugging information should be analyzed theoretically as follows:
Press the return key:
KeyBackClicked !!!
DelegateTest release !!
Press the return key again:
KeyBackClicked !!!
DelegateTest release !!
......
The debugging information is as follows:
Press the return key:
KeyBackClicked !!!
Press the return key again:
KeyBackClicked !!!
Exit the game:
DelegateTest release !!
DelegateTest release !!
See the difference! What is the result of this phenomenon. That is, the current scenario has not been removed and can receive touch. When more than one scenario appears, its logic will be disordered !!!
Now the code is ported to 3.x.

The 3. x code is as follows:
. H DelegateTest (); virtual ~ DelegateTest (); virtual void onKeyReleased (cocos2d: EventKeyboard: KeyCode keyCode, cocos2d: Event * event) override; cocos2d: EventListenerKeyboard * handle; // The keyboard listener cocos2d :: eventListenerTouchOneByOne * m_touchListener; // touch listener. cppDelegateTest: DelegateTest () {} DelegateTest ::~ DelegateTest () {log ("DelegateTest release !! ");} Bool DelegateTest: init () {if (! CCLayer: init () {return false;} Keys = EventListenerKeyboard: create (); keys-> retain (); m_keyboardListener-> onKeyReleased = CC_CALLBACK_2 (DelegateTest: onKeyReleased, this); getEventDispatcher ()-> listener (m_keyboardListener, this); // setKeypadEnabled (true); ---- m_touchListener = EventListenerTouchOneByOne: create (); m_touchListener-> Retain (); m_touchListener-> onTouchBegan = CC_CALLBACK_2 (DelegateTest: onTouchBegan, this); m_touchListener-> identifier = [&] (cocos2d: Touch * touch, cocos2d :: event * event) {return true;}; getEventDispatcher ()-> listener (m_touchListener, this); return true;} void DelegateTest: onKeyReleased (cocos2d: EventKeyboard :: keyCode keyCode, cocos2d: Event * event) {Scene * scene Layer = DelegateTest: scene (); ctor: getInstance ()-> replaceScene (sceneLayer); log ("onKeyReleased !!! ");}

The function is exactly the same as described above. The debugging information is as follows:
OnKeyReleased !!!
DelegateTest release !!
OnKeyReleased !!!
DelegateTest release !!
It is consistent with the expected result. Why are these two different situations. This is because in 2.x, a listener event is registered, so we must deregister the listener event accordingly. 3. x is the creation of the listener object. In this way, the object will be automatically removed during the analysis. In terms of design, 3. x ratio 2. x is easier and error-free.
So how can we change the result of 2.x so that it is consistent with our theory? Here you can change the keyBackClicked () Code of the function:

void DelegateTest::keyBackClicked(){    CCScene* sceneGame = DelegateTest::scene();    CCDirector::sharedDirector()->replaceScene(sceneGame);    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);    CCLog("keyBackClicked!!!");}

Of course, in theory, when 3.x exits the scenario, it also needs to remove the listener object. The function is release (), which saves some time. It may be said that simply adding a sentence in 2.x and forgetting to log out is a problem of your own. The first thing to note here is that when there are many layers, unlogging the touch is not a simple task. At the same time, the exit scenario may be called in one subclass; or in another layer, disable all layers in the current scenario, such as pausing the interface. On the other side, when popScene and pushScene are used, this is even more a disaster !!! A good design for a game engine should help developers avoid pitfalls. This is also why when rendering in the cocos2d-x, why not specifically open a thread to achieve separation of logic and presentation. This is too difficult, and will lead to a lot of detailed problems, which virtually increases the difficulty of developers. Of course, with the development of engines in the future, I believe this problem will be solved. For details, refer to Wang Zhe's answer: http://www.zhihu.com/question/27642527/answer/10978748.
For the above Code details, you can access the author githup: https://github.com/DionysosLai/CocoSamples in the "Delegate Test" content. The first article in 2015 marks the New Year and wishes you a better New Year !!! -Ps: The first time I used the markdown editor, I felt cute !!!

Related Article

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.