COCOS2DX event interaction Processing
I. Single Point of touch
To create and listen to a single touch event, follow these steps:
1. Create a space such as labelTTF and initialize it. Add the control to the layer.
2. Set an event listener and define and implement its callback function.
3. Finally, let the Director Add the listener defined above to the listener in the way corresponding to the listener and the listener event.
Size visibleSize = Director: getInstance ()-> getVisibleSize (); Vec2 origin = Director: getInstance ()-> getVisibleOrigin (); auto label = LabelTTF :: create ("click me", "", 36); addChild (label); label-> setPosition (visibleSize. width/2, visibleSize. height/2); auto * listener = EventListenerTouchOneByOne: create (); listener-> onTouchBegan = [] (Touch * t, Event * e) {log ("ontouchbegan "); return false ;}; Director: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, label );
2. Determine the touch target
When implementing a touch event, we often require that the event only appears when you click in a specific area. This requires you to judge in the Code whether the point you touch is within the scope of the touch target.
In a touch event, the Lintener listener specifies a touch callback function for the label event. The callback function contains a parameter of the e type and the event type. The function uses e-> getcurrenttarget () the function can get our touch target, that is, the label, and then obtain the border of the touch target through the getBoundingBox () method. Finally, determine whether it contains our touch points.
ContainsPoint (t-> getLocation ())
T-> getLocation () displays the position of the touch point. Note that if you use labelttf to create a label space, the left side of the obtained vertex may be incorrect. Therefore, use label: create (); To create the function. In this case, a cocos Bug occurs.
Auto label = Label: create ("click me", "", 36); addChild (label); label-> setPosition (visibleSize. width/2, visibleSize. height/2); auto * listener = EventListenerTouchOneByOne: create (); listener-> onTouchBegan = [] (Touch * t, Event * e) {if (e-> getCurrentTarget ()-> getBoundingBox (). containsPoint (t-> getLocation () {log ("ontouchbegan");} return false ;};
Iii. Event Transmission
To put it bluntly, the event transfer mechanism means that the events you trigger in the game are in sequence.
Generally, there are the following types of events:
std::function
onTouchBegan; std::function
onTouchMoved; std::function
onTouchEnded; std::function
onTouchCancelled;
Literally, we can basically determine what these events can do. However, we can find that only one of the several event functions is onTouchBegan. It requires that the return value be of the Bool type. That is to say, the Ontouchbegan event should be executed first, because you must press your mouse to execute the drag, pop up, cancel, and other actions, assume that I have the following code:
listener->onTouchBegan = [](Touch* t, Event* e){if(e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation())){log("ontouchbegan");}return false;};listener->onTouchMoved = [](Touch *t, Event* e){log("ontouchmoved");};
If onTouchBegan returns false in the function, it will not trigger the subsequent events. However, if it is set to true, it indicates that the events after division can be continued.
4. Monitor physical button events
Each physical button on the phone is the same as that on the computer, and an integer represents them.
The first step is to create a listener specifically used to listen to physical key events:
Auto listener = EventListenerKeyboard: create ();
Then, you can specify the onKeyReleased function and add the listener to the director's management. When debugging on a real machine, you can see that each time you press a key, the corresponding key value is displayed.
Auto listener = EventListenerKeyboard: create (); listener-> onKeyReleased = [] (EventKeyboard: KeyCode code, Event * e) {log ("key code % d ", code); // display the key value switch (code) {case EventKeyboard: KeyCode: KEY_ESCAPE: // use the switch statement to let different buttons execute different actions Director :: getInstance ()-> end (); break; default: break ;}}; Director: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, this );