Cocos2d-x 3.2 Lua Demo sample clickandmovetest (click on Move Test)

Source: Internet
Author: User

Cocos2d-x 3.2 Lua Demo sample clickandmovetest (click on Move test)

this blog introduces the Cocos2d-x 3.2Lua Demo sample, click-To-Move sample, in this sample you can get how to create a single touch event and register event listener callback method.



Demo Sample 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 color layer, yellow local bglayer = cc.    Layercolor:create (cc.c4b (255,255,0,255)) Layer:addchild (Bglayer,-1)--join? Layer:addchild (Sprite, 0, Ktagsprite)--set sprite position to (20,150) sprite:setposition (CC.P (20,150))--run Jump action, first number is duration, second The number of participants is the position, the third number is the height of the jump, the fourth sprite:runaction (CC). Jumpto:create (4, CC.P (300,48), 100, 4))--the background layer runs infinitely repeated action sequences, 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 nodes by tag local s = Layer:getchildbytag (ktagsprite) s:stopallactions ()--Stop all actions-- To run the move action, move to the point where you clicked 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-runs the rotation of the action s:runaction (CC. Rotateto:create (1, at)) end--single-touch listener local listener = CC. Eventlistenertouchonebyone:create ()--register two callback monitoring 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 where Eventdispatcher:addeventlistene Rwithscenegraphpriority (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, one for multi-touch, and one Touch event for this blog example. Eventlistenertouchonebyone class
--single-touch listener    Local listener = cc. Eventlistenertouchonebyone:create ()    --register two callback monitoring methods    Listener:registerscripthandler (ONTOUCHBEGAN,CC. Handler.event_touch_began)    Listener:registerscripthandler (ontouchended,cc. handler.event_touch_ended)    Local eventdispatcher = Layer:geteventdispatcher ()--time dispatcher-    -binds the touch event to the layer where    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, followed by the first to be touched.

Assuming that the blog is useful to you, please go to the following link for the wizard to cast a sacred vote. http://vote.blog.csdn.net/Article/Details?articleid=38272837

Cocos2d-x 3.2 Lua Demo sample clickandmovetest (click on Move Test)

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.