cocos2d-x 3.2 Lua sample clickandmovetest (click Mobile Test)
This blog describes the Cocos2d-x 3.2Lua example in the click-to-move example, in this example you can get how to create a single-touch event and register an event listener callback method.
Example code:
--[[clickandmovetest.lua click and move]]----get the screen size local size = CC. Director:getinstance (): getwinsize () Local layer = nil--layer local Ktagsprite = 1--Goblin flag local function Initwithlayer () Loca L sprite = cc. Sprite:create (S_ppathgrossini)--Add a color layer, yellow local bglayer = cc. Layercolor:create (cc.c4b (255,255,0,255)) Layer:addchild (Bglayer,-1)--Add Layer:addchild (Sprite, 0, Ktagspri TE)--Set the sprite position to (20,150) sprite:setposition (CC.P (20,150))--Perform the jump action, the first parameter is the duration, the second parameter is the position, the third parameter is the height of the jump, the fourth parameter jumps the number of s Prite:runaction (CC. Jumpto:create (4, CC.P (300,48), 100, 4))--The background layer performs an infinitely repetitive sequence of actions, first fade in, then fade out bglayer:runaction (CC. Repeatforever:create (CC. Sequence:create (CC. Fadein:create (1), CC. Fadeout:create (1)))--Touch start local function Ontouchbegan (touch, event) return True end--touch End Local function ontouchended (Touch, event)--Get the click position local location = Touch:getlocation () --Get child node according to tag local s = Layer:getchildbytag (ktagsprite) s:stopallactions ()--Stop all actions-- Move the action to the clicked position S:runaction (CC. Moveto:create (1, CC.P (location.x, LOCATION.Y))) Local PosX, PosY = S:getposition ()--Get the location of the genie local o = Loca TION.X-POSX-X-axis distance Local A = location.y-posy-y axis distance Local at = Math.atan (o/a)/Math.PI * 180.0-- To find the angle, the inverse tangent function for radians/π*180.0--1 radians = 180/π, 1 degrees =π/180--click on the position below if a < 0 then--click on the location on the left If o < 0 then at = math.abs + +/---click on the right at = 180 -Math.Abs (at) end end-performs the rotation of the action s:runaction (CC. Rotateto:create (1, at)) end--single-touch listener local listener = CC. Eventlistenertouchonebyone:create ()--register two callback listener methods Listener:registerscripthandler (ONTOUCHBEGAN,CC. Handler.event_touch_began) Listener:registerscripthandler (ontouchended,cc. Handler.event_touch_eNDED) Local eventdispatcher = Layer:geteventdispatcher ()--time dispatcher--bind touch event to layer Eventdispatcher:addeventlistenerw Ithscenegraphpriority (listener, layer) return layerend----------------------------------Click and Move Test--------------------------------function Clickandmovetest () cclog ("Clickandmovetest") Local scene = cc. Scene:create () layer = CC. Layer:create () Initwithlayer () scene:addchild (layer) Scene:addchild (Createbackmenuitem ()) return sceneend
Cocos2d-x has two touch events, one for single touch and one for multi-touch, this blog example uses a single touch event.
Eventlistenertouchonebyone class
--single-touch listener Local listener = cc. Eventlistenertouchonebyone:create () --register two callback listener methods Listener:registerscripthandler (ONTOUCHBEGAN,CC. Handler.event_touch_began) Listener:registerscripthandler (ontouchended,cc. handler.event_touch_ended) Local eventdispatcher = Layer:geteventdispatcher ()--time dispatcher- -bind touch event to layer Eventdispatcher:addeventlistenerwithscenegraphpriority (listener, layer)
The last method binds the node to the listener, and the lower the touch priority, the more touched it is first. The same priority, after the addition of the first touch.