Cocos2dx development and growth road 004

Source: Internet
Author: User
1. An overview of games, programs, and user interaction is meaningful. Interaction on a mobile phone can be divided into two parts: Click and input. Click is more important, almost all interactions in the game. In Cocos2d-x 3.0, the dispatch mechanism was changed. Two new interaction modes are added: listener and touchevent callback. In addition, the click function callback in the previous version forms a relatively complete interaction mode with the touch message response of the rewriting layer. First, go to a demo image: 2. Four types of clicks 1. Function callback is the simplest form of response, which has been used for click processing in menuitem for a long time. In the new version, some minor changes have taken place here. We can see that the code in the generated program is as follows: [CPP]View plaincopyprint?
  1. // A selector callback
  2. Void menuclosecallback (Object * psender );
  3. Auto closeitem = menuitemimage: Create ("closenormal.png", "closeselected.png ",
  4. Cc_callback_1 (helloworld: menuclosecallback, this ));
  5. Void helloworld: menuclosecallback (Object * psender)
  6. {
  7. Director: getinstance ()-> end ();
  8. # If (cc_target_platform = cc_platform_ios)
  9. Exit (0 );
  10. # Endif
  11. }

The cc_callback_1 macro binds a function with an object. 1 indicates that this function has a parameter. When you click this button, the callback function is called.

Except for the changes in the Form Based on C ++ 11, they are used in the same way as before.


This method has been abandoned and can be replaced by the fourth method.

2. layer touch message response

Although the underlying dispatch is rewritten, the use of this layer is not significant. We also need to rewrite:


[CPP]View plaincopyprint?
  1. // Single point of response
  2. Virtual bool ontouchbegan (touch * touch, event * event) override;
  3. Virtual void ontouchmoved (touch * touch, event * event) override;
  4. Virtual void ontouchended (touch * touch, event * event) override;
  5. Virtual void ontouchcancelled (touch * touch, event * event) override;
  6. // Multi-point response
  7. Virtual bool ontouchesbegan (touch * touch, event * event) override;
  8. Virtual void ontouchesmoved (touch * touch, event * event) override;
  9. Virtual void ontouchesended (touch * touch, event * event) override;
  10. Virtual void ontouchescancelled (touch * touch, event * event) override;


Rewrite these functions to process layer clicks. Of course, we need:


[CPP]View plaincopyprint?
  1. Settouchenabled (true ).


There is also a small change. For single-touch responses, you can call:

[CPP]View plaincopyprint?
  1. // Set to single-point response
  2. Settouchmode (touch: dispatchmode: one_by_one );
  3. // Set to multi-point response (default)
  4. Settouchmode (touch: dispatchmode: all_at_once );


Instead of setting delegate.


3. touchevent response this is the newly added response method. It is mainly used in UIWidget. It can be seen as an extension of function callback, providing more Response Processing possibilities. The usage is roughly as follows: [CPP]View plaincopyprint?
  1. // Statement
  2. Void touchbutton (Object * object, toucheventtype );
  3. // Link to the control
  4. Uibutton-> addtoucheventlistener (this, toucheventselector (helloworld: touchbutton ));
  5. // Implementation
  6. Void helloworld: touchbutton (Object * object, toucheventtype type)
  7. {
  8. Labelttf * label;
  9. Switch (type)
  10. {
  11. Case toucheventtype: touch_event_began:
  12. Label = static_cast <labelttf *> (getchildbytag (11 ));
  13. Label-> setstring ("press the button ");
  14. Break;
  15. Case toucheventtype: touch_event_moved:
  16. Label = static_cast <labelttf *> (getchildbytag (11 ));
  17. Label-> setstring ("press the button to move ");
  18. Break;
  19. Case toucheventtype: touch_event_ended:
  20. Label = static_cast <labelttf *> (getchildbytag (11 ));
  21. Label-> setstring ("release button ");
  22. Break;
  23. Case toucheventtype: touch_event_canceled:
  24. Label = static_cast <labelttf *> (getchildbytag (11 ));
  25. Label-> setstring ("unclick ");
  26. Break;
  27. Default:
  28. Break;
  29. }
  30. }

Because all the uiwidgets must be added to uilayerWidgets used as the UI are usually at the top layer, so we can "basically" think that this method will take precedence over other methods to process click messages. Because uilayer also has a hierarchical change, such as its relationship with menuitem. So "basically ".

4. Listener message RESPONSE METHOD

This implementation is also newly added. It is more like a layered filter of clicks. When you click, filter in the listener team. Each listener checks whether its own touch message response is triggered. Layer-by-layer filtering, and finally the touch message response to the layer.

I think it was designed to provide a set of custom click responses for any sprite. However, such an implementation still requires many conditions to be judged and cannot be controlled. There may be some deviations in my understanding.

It is designed as a global click Response Control. The specific usage is roughly as follows:



[CPP]View plaincopyprint?
  1. // Auto dispatcher = eventdispatcher: getinstance ();
  2. // Auto mylistener = eventlistenertouch: Create (touch: dispatchmode: one_by_one );
  3. Auto dispatcher = Director: getinstance ()-> geteventdispatcher ();
  4. Auto mylistener = eventlistenertouchonebyone: Create ();
  5. // If this sentence is not added, the message will still be passed down
  6. Mylistener-> setswallowtouches (true );
  7. Mylistener-> ontouchbegan = [=] (touch * touch, event * event)
  8. {
  9. // Some check
  10. If (PASS)
  11. {
  12. Return true;
  13. }
  14. Return false;
  15. };
  16. Mylistener-> ontouchmoved = [=] (touch * touch, event * event)
  17. {
  18. // Do something
  19. };
  20. Mylistener-> ontouchended = [=] (touch * touch, event * event)
  21. {
  22. // Do something
  23. };
  24. Dispatcher-> addeventlistenerwithscenegraphpriority (mylistener, mysprite1 );
  25. Dispatcher-> addeventlistenerwithscenegraphpriority (mylistener, mysprite2 );

The principle is to check the listener list in Dispatcher, for example, mylistener or other added listener. Then, each listener checks the items in the listener to see if the conditions can be met, for example, mysprite1 and mysprite2. Then perform the corresponding operation. In this case, when there are many controls, I wonder if this double-linked table check operation will affect the performance of each event?

Cocos2dx development and growth road 004

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: 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.