Transferred from: http://blog.csdn.net/fansongy/article/details/12716671
Copyright NOTICE: This article is Songyang (blog.csdn.net/fansongy) original article, reprint must indicate the source.
Directory (?) [+]1. Overview
The game is good, the procedure is good, only can interact with the user to have the meaning. The interactions on the phone can be broadly divided into two parts: click and enter. One click is more important, almost all the interactions in the game. In Cocos2d-x 3.0, the dispatch mechanism was changed. Two new forms of interaction have been added: Listener and touchevent callbacks. Plus the click Function callback from the previous version, and the touch message response to rewrite the layer, constitutes a relatively complete interaction pattern. First, a diagram of the demo:
2. Four types of clicks
1. Function CallbackFunction callbacks are the simplest form of response and have been used for click Processing in MenuItem. In the new version, minor changes have taken place here. We can see that the relevant code in the generated program is this:
[CPP]View Plain copy print? A selector callback void Menuclosecallback (object* psender); Auto Closeitem = menuitemimage::create ("Closenormal.png", "Closeselected.png", Cc_callback_1 (Hell Oworld::menuclosecallback, this)); void Helloworld::menuclosecallback (object* psender) {director::getinstance ()->end (); #if (Cc_target_platform = = Cc_platform_ios) exit (0); #endif}
Where Cc_callback_1 macros are binding functions to objects, 1 means that the function has a parameter. This callback function is called when the button is clicked.
In addition to this form of change based on c++11, the use method is the same as before.
This method has been discarded and can be substituted using the fourth method. 2, layer of touch message response
Although the underlying dispatch has been rewritten, it has little effect on the use of this layer. We also need to rewrite:
[CPP] View plain copy print? 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; //multipoint 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 handle the click of the layer. Of course, we need:
[CPP] view plain copy print? Settouchenabled (True).
There's also a minor change. For a single-touch response, you can call:
[CPP] view plain copy print? Set to single-point response Settouchmode (Touch::D ispatchmode::one_by_one); Set to multi-point response (default) Settouchmode (Touch::D ispatchmode::all_at_once);
To be set up without having to do it in the same way as the setup delegate.
3, touchevent response This is the new response mode added. It is primarily used on UI widgets. It can be seen as an extension of a function callback, providing the possibility for more response processing. The use method is roughly: