Click interaction in four ways. Click interaction in four ways.

Source: Internet
Author: User

Click interaction in four ways. Click interaction in four ways.
1. Overview
Games, programs, and users can only interact with each other. 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 clicks
1. Function callback is the simplest form of response. It 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?


3. Instance

I simply did not practice fake tricks, so I wrote the above Demo:

You can click the background area to move the entire scenario. You can click a small blue box to move it transparently. Click the blue button to change the text and display status. Click the button in the lower right corner to exit the program.

For project configuration, see:

Cocos2d-x 3.0 development (16) cocos2dx-3.0beta edition creates new projects and loads CocoStudio export files

4. Summary

Depending on the interaction needs, selecting different implementation methods will be more conducive to our maintenance and expansion. The corresponding example can be downloaded below.


Demo download: http://download.csdn.net/detail/fansongy/6399291 do not resource points, feel so tired under the "TOP "~

Demo For Beta2 download: http://download.csdn.net/detail/fansongy/6892047


This blog from a shura Road, reproduced please indicate the source, prohibited for commercial purposes: http://blog.csdn.net/fansongy/article/details/12716671


In the four statuses of the button, click, and

Press to a position without returning. Click Yes to display the button.

In the 11 response types of interaction icons, authorware cannot be set to permanent.

You can test it on your own. Congratulations.

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.