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?
- // A selector callback
- Void menuCloseCallback (Object * pSender );
- Auto closeItem = MenuItemImage: create ("CloseNormal.png", "CloseSelected.png ",
- CC_CALLBACK_1 (HelloWorld: menuCloseCallback, this ));
- Void HelloWorld: menuCloseCallback (Object * pSender)
- {
- Director: getInstance ()-> end ();
- # If (CC_TARGET_PLATFORM = CC_PLATFORM_IOS)
- Exit (0 );
- # Endif
- }
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?
- // Single point of response
- Virtual bool onTouchBegan (Touch * touch, Event * event) override;
- Virtual void onTouchMoved (Touch * touch, Event * event) override;
- Virtual void onTouchEnded (Touch * touch, Event * event) override;
- Virtual void onTouchCancelled (Touch * touch, Event * event) override;
- // Multi-point response
- Virtual bool onTouchesBegan (Touch * touch, Event * event) override;
- Virtual void onTouchesMoved (Touch * touch, Event * event) override;
- Virtual void onTouchesEnded (Touch * touch, Event * event) override;
- Virtual void onTouchesCancelled (Touch * touch, Event * event) override;
Rewrite these functions to process layer clicks. Of course, we need:
[Cpp]View plaincopyprint?
- SetTouchEnabled (true ).
There is also a small change. For single-touch responses, you can call:
[Cpp]View plaincopyprint?
- // Set to single-point response
- SetTouchMode (Touch: DispatchMode: ONE_BY_ONE );
- // Set to multi-point response (default)
- 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?
- // Statement
- Void touchButton (Object * object, TouchEventType );
- // Link to the control
- UiButton-> addTouchEventListener (this, toucheventselector (HelloWorld: touchButton ));
- // Implementation
- Void HelloWorld: touchButton (Object * object, TouchEventType type)
- {
- LabelTTF * label;
- Switch (type)
- {
- Case TouchEventType: TOUCH_EVENT_BEGAN:
- Label = static_cast <LabelTTF *> (getChildByTag (11 ));
- Label-> setString ("press the button ");
- Break;
- Case TouchEventType: TOUCH_EVENT_MOVED:
- Label = static_cast <LabelTTF *> (getChildByTag (11 ));
- Label-> setString ("press the button to move ");
- Break;
- Case TouchEventType: TOUCH_EVENT_ENDED:
- Label = static_cast <LabelTTF *> (getChildByTag (11 ));
- Label-> setString ("release button ");
- Break;
- Case TouchEventType: TOUCH_EVENT_CANCELED:
- Label = static_cast <LabelTTF *> (getChildByTag (11 ));
- Label-> setString ("unclick ");
- Break;
- Default:
- Break;
- }
- }
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?
- // Auto dispatcher = EventDispatcher: getInstance ();
- // Auto myListener = EventListenerTouch: create (Touch: DispatchMode: ONE_BY_ONE );
- Auto dispatcher = Director: getInstance ()-> getEventDispatcher ();
- Auto myListener = EventListenerTouchOneByOne: create ();
- // If this sentence is not added, the message will still be passed down
- MyListener-> setSwallowTouches (true );
- MyListener-> onTouchBegan = [=] (Touch * touch, Event * event)
- {
- // Some check
- If (pass)
- {
- Return true;
- }
- Return false;
- };
- MyListener-> onTouchMoved = [=] (Touch * touch, Event * event)
- {
- // Do something
- };
- MyListener-> onTouchEnded = [=] (Touch * touch, Event * event)
- {
- // Do something
- };
- Dispatcher-> addEventListenerWithSceneGraphPriority (myListener, mySprite1 );
- 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.