Virtual void ccTouchesCancelled (cocos2d: CCSetPTouches, cocos2d: CCEventPEvent );
In 3.0, you only need to create a multi-touch event listener and add it to the event distributor.
The following code adds three buttons to an interface, which are blocked by each other and trigger a touch event:
| 12345678 |
// Create button sprite1 = Sprite: create ("Images/CyanSquare.png"); sprite1-> setPosition (origin + Point (size. width/2, size. height/2) + Point (-80, 80); addChild (sprite1, 10); // sprite2... // sprite3... |
After creating the button genie, create a single point of touch event listener and complete corresponding logic processing.
| 123456789101112131415161718192021222324252627282930 |
// Create a single-touch auto listener1 = EventListenerTouchOneByOne with the event listener type OneByOne: create (); // you can specify whether the event is swallowed up, when the onTouchBegan method returns true, the listener1-> setSwallowTouches (true) is swallowed; // use lambda to implement the onTouchBegan Event Callback Function listener1-> onTouchBegan = [] (Touch * touch, * event) -> bool {// obtain the target autotarget = static_cast bound to the event (Event-> getCurrentTarget (); // obtain the coordinate Point locationInNode = target-> convertToNodeSpace (touch-> getLocation () of the corresponding button of the current click Point ()); sizes = target-> getContentSize (); Rect rect = Rect (0, 0, s. width, s. height); // click the range to determine if (rect. containsPoint (locationInNode) {log ("sprite began... x = % f, y = % f ", locationInNode. x, locationInNode. y); target-> setOpacity (180); returntrue;} returnfalse;}; // trigger listener1-> onTouchMoved = [] (Touch * touch, Event * event) {...}; // click the end of the Event to process listener1-> onTouchEnded = [=] (Touch * touch, event * Event ){...}; |
Add the event listener to the event distributor.
| 1234 |
// Add the listener _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener1, sprite1); _ eventDispatcher-> handler (listener1-> clone (), sprite2 ); _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener1-> clone (), sprite3 ); |
In the above Code, _ eventDispatcher is the attribute of Node. It manages the distribution of all events on the current Node (scenario, layer, Genie, etc. However, it is a reference of the singleton mode value. In the Node constructor, it is obtained through Director: getInstance ()-> getEventDispatcher ();. With this attribute, you can easily process events.
Note: When you use listener1 again, you need to use the clone () method to create a new clone, because when you use the addEventListenerWithSceneGraphPriority or addEventListenerWithFixedPriority method, A registered tag will be added to the currently used event listener, so that it cannot be added multiple times. In addition, it is very important to manually remove FixedPriority listener after it is added. SceneGraphPriority listener is bound to Node and will be removed from Node destructor. For specific examples, refer to the tests provided by the engine.
We can remove an added listener using the following methods.
_ EventDispatcher-> removeEventListener (listener );
You can also remove all listeners in the current event distributor using the following method.
_ EventDispatcher-> removeAllEventListeners ();
When removeAll is used, all the listeners on this node will be removed. We recommend that you delete the nodes in the specified way. After removeAll, the menu cannot respond. Because it also needs to accept touch events.
Keyboard RESPONSE event
The keyboard response event and processing touch events use the same processing method. The code below demonstrates how to handle the keyboard response event:
| 1234567891011121314151617 |
// Initialize and bind auto listener = EventListenerKeyboard: create (); listener-> onKeyPressed = encrypted (KeyboardTest: onKeyPressed, this); listener-> onKeyReleased = CC_CALLBACK_2 (KeyboardTest :: onKeyReleased, this); _ eventDispatcher-> listener (listener, this); // voidKeyboardTest: onKeyPressed (EventKeyboard: KeyCodekeyCode, Event * event) {log ("Key with keycode % d pressed", keyCode);} voidKeyboardTest: onKeyReleased (EventKeyboard: KeyCodekeyCode, Event * event) {log ("Key with keycode % d released", keyCode );} |
Mouse RESPONSE event
In 3.0, more Mouse capture events are distributed, which can enrich the user experience of our games on different platforms.
The following code implements the mouse response to an event:
| 123456789101112131415 |
// Create the listener _ mouseListener = EventListenerMouse: create (); // time response logic _ mouseListener-> onMouseMove = [=] (Event * event) {EventMouse * e = (EventMouse *) event; stringstr = "Mouse Down detected, Key:"; str + = tostr (e-> getMouseButton ()); //...}; _ mouseListener-> onMouseUp = [=] (Event * event ){...}; _ mouseListener-> onMouseDown = [=] (Event * event ){...}; _ mouseListener-> onMouseScroll = [=] (Event * event ){...}; // Add it to the event distributor _ eventDispatcher-> addEventListenerWithSceneGraphPriority (_ mouseListener, this ); |
Custom events
The above are built-in event types, which are automatically triggered by the system, such as touch screen and keyboard response. In addition, a custom event is provided, in short, it is not automatically triggered by the system, but human interference, as follows:
| 123456789 |
_ Listener = EventListenerCustom: create ("game_custom_event1", [=] (EventCustom * event) {std: stringstr ("Custom event 1 received,"); char * buf = static_cast (Event-> getUserData (); str + = buf; str + = "times"; statusLabel-> setString (str. c_str () ;}); _ eventDispatcher-> addEventListenerWithFixedPriority (_ listener, 1 ); |
The above defines a "Custom Event listener", implements the relevant logic, and adds it to the event distributor. The above Custom Event will be triggered by the following code:
| 1234567891011 |
Staticint count = 0; ++ count; char * buf = new char [10]; sprintf (buf, "% d", count); EventCustomevent ("game_custom_event1"); event. setUserData (buf); if (...) {_ eventDispatcher-> dispatchEvent (& event);} CC_SAFE_DELETE_ARRAY (buf ); |
Define an EventCustom and set its UserData. Manually distribute the event through _ eventDispatcher-> dispatchEvent (& event); To trigger the previously implemented logic.
Accelerator event
In addition to the touch, a very important input source on a mobile device is the direction of the device. Therefore, most devices are equipped with an accelerometer to measure the gravity direction of the device when it is still or at a constant speed.
Gravity sensing is derived from a mobile accelerator. It usually supports acceleration in the X, Y, and Z directions. It is also called a three-way accelerator. In actual application, you can calculate the angle or direction of the mobile phone tilt based on the strength of the three directions.
3.0, under the new event mechanism, we need to create an accelerator listener EventListenerAcceleration. The static create method has an Acceleration parameter. Acceleration is a class that contains three Acceleration directions obtained by the accelerator. The related code is as follows:
| 1234567891011 |
ClassAcceleration {public: doublex; doubley; doublez; doubletimestamp; Acceleration (): x (0), y (0), z (0), timestamp (0 ){}}; |
The acceleration of each direction in this class is one gravity acceleration.
Before using the accelerator event listener, you must enable this hardware device:
Device: setAccelerometerEnabled (true );
Create a listener. When creating a callback function, you can use a lambda expression to create an anonymous function, or bind an existing function logic implementation, as shown below:
| 1234 |
Autolistener = EventListenerAcceleration: create ([=] (Acceleration * acc, Event * event) {// logical code segment}); _ eventDispatcher-> addEventListenerWithSceneGraphPriority (listener, this ); |