This article began to talk about single-touch events, we used UIPushButton in front of the menu that we used the touch event, but we do not feel, we basically do not need to analyze touch messages. In this article we use an elf to imitate the effect of the button, to enhance understanding
As for the principle, Liao in the document is very clear, will not be redundant.
Quick Touch Mechanism (dot Me dot Me (*^__^*))
Generally, when we press the button, always find that the button is pressed down, the button will be reduced, released, the button will restore the original size.
function Myscene:ctor () Local sprite = Display.newsprite ("hello.png") sprite:align (display. center,display.cx,display.cy) Sprite:addto (self)--The following is the use of a single touch sprite:settouchenabled (true) -- Sets whether the genie can touch Sprite:settouchmode (cc). Touch_mode_one_by_one)--touch mode. We pass in a single touch mode into the Sprite:addnodeeventlistener (CC. Node_touch_event, Function (event)- -The key, here is the event type and callback function, processing the message in the callback function local x, y, PrevX, Prevy = Event.x, Event.y, EVENT.PREVX, Event.prevy if Event.name = = "began" Thensprite:setscale (0.9) ElseIf event.name = = "Moved" Then --Sprite:setposition (CC.P (x, y) elseif Event.name = = "Ended" then Sprite:setscale (1) end return TrueEnd) End
In the code, I've commented on the key points.
For this CC. Node_touch_event, this is the engine-defined event, and here's the COCOS2DX engine time, and we'll use the touch event
--COCOS2DX engine level event C. Node_event = 0c. Node_enter_frame_event = 1c. Node_touch_event = 2c. Node_touch_capture_event = 3c. Menu_item_clicked_event = 4c. Accelerate_event = 5c. Keypad_event = 6c. Node_touch_capturing_phase = 0c. Node_touch_targeting_phase = 1
Another question is the event, how do we know what's in it, don't be afraid, quick has encapsulated this function----dump function
This function can help us to output all the contents of the input parameters, of course, if the parameter mosaic layer is too deep (this function defaults to 3 layers), you can wear the third parameter to the number of layers you want to output. Specific use, you can see the source code.
Let's add a dump to the callback function (event)
Well, look very clearly. Below you see our simple button
Quick Cocos2dx-lua (V3.3R1) Learning Note (ix)----single-touch event of the event, let's use the sprite to imitate a button.