Cocos2d-x 3.2 Lua example ClickAndMoveTest (click to move the test)

Source: Internet
Author: User

Cocos2d-x 3.2 Lua example ClickAndMoveTest (click to move the test)
Cocos2d-x 3.2 Lua example ClickAndMoveTest (click to move the test)

This blog introduces the example of clicking to move in the Cocos2d-x 3.2Lua example, in which you can get how to create a single point of touch event and register the event listening callback method.



Sample Code:

-- [ClickAndMoveTest. lua click and move] ---- get the screen size local size = cc. director: getInstance (): getWinSize () local layer = nil -- layer local kTagSprite = 1 -- the sprite marks the local function initWithLayer () local sprite = cc. sprite: create (s_pPathGrossini) -- add the color layer, yellow local bgLayer = cc. layerColor: create (cc. c4b (255,255, 0,255) layer: addChild (bgLayer,-1) -- add layer: addChild (sprite, 0, kTagSprite) -- set the sprite location to (20,150) sprite: setPosition (cc. p (20,150) -- executes the hop action. The first parameter is duration, the second parameter is position, the third parameter is the hop height, and the fourth parameter is the number of hops sprite: runAction (cc. jumpTo: create (4, cc. p (100, 48), 4) -- executes an infinitely repeated action sequence on the background layer, fades 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 location local location = touch: getLocation () -- Obtain the subnode local s = layer: getChildByTag (kTagSprite) s: stopAllActions () based on the tag () -- stop all actions -- execute the move action and move it 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 = location. x-posX -- the distance from the x axis is local a = location. y-posY -- the distance from the y axis is local at = math. atan (o/a)/math. pi * 180.0 -- angle, arc tangent function for radians/π * 180.0 -- 1 radian = 180/π, 1 degree = π/180 -- click position below if a <0 then -- click position on the left if o <0 then at = 180 + math. abs (at) else -- click at = 180-math on the right. abs (at) end -- execute the rotation action s: runAction (cc. rotateTo: create (1, at) end -- listener local listener = cc for single point of touch. eventListenerTouchOneByOne: create () -- register two callback listening Methods: listener: registerScriptHandler (onTouchBegan, cc. handler. EVENT_TOUCH_BEGAN) listener: registerScriptHandler (onTouchEnded, cc. handler. EVENT_TOUCH_ENDED) local eventDispatcher = layer: getEventDispatcher () -- time dispatcher -- bind the touch event to the eventDispatcher: addEventListenerWithSceneGraphPriority (listener, layer) in the 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 kinds of touch events, one is single touch, one is multi-touch, this blog example uses single touch events. EventListenerTouchOneByOne class
-- Local listener = cc. eventListenerTouchOneByOne: create () -- register two callback listening Methods: listener: registerScriptHandler (onTouchBegan, cc. handler. EVENT_TOUCH_BEGAN) listener: registerScriptHandler (onTouchEnded, cc. handler. EVENT_TOUCH_ENDED) local eventDispatcher = layer: getEventDispatcher () -- time dispatcher -- bind the touch event to the eventDispatcher: addEventListenerWithSceneGraphPriority (listener, layer) in the layer)

The last method binds the node to the listener, and the lower the touch priority, the more touched. If the priority is the same, the last added is triggered first.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.