Cocos2dx + Lua registration event function details

Source: Internet
Author: User
Tags addchild

Coocs2dx version 3.1.1

Registerscripttouchhandler registers a touch screen event

Registerscripttaphandler registers a click event

Registerscripthandler registers basic events, including entry and exit events on the touch screen layer.

Registerscriptkeypadhandler registers Keyboard Events

Registerscriptacceleratehandler registers acceleration events


Registerscripttouchhandler (single point or multiple points can be set)

Function gamewindow: addlayertoucheventmethod1 () Local function ontouchevent (eventtype, x, y) -- log ("eventtype = ".. tostring (eventtype) If eventtype = "began" then -- Return true return ontouchbegan (touch, event) elseif eventtype = "Moved" then ontouchmoved (touch, event) elseif eventtype = "ended" then ontouchended (touch, event) end config. bottomlayer: settouchenabled (true) config. bottomlayer: registerscripttouchhandler (ontouchevent) End

Registerscripttaphandler registers a click event

function gameWindow:addBtn()    local btn = cc.MenuItemImage:create("white.png", "black.png", "black.png")    btn:setPosition(320, 160)    local function btnClick()        log("btnClick")    end    btn:registerScriptTapHandler(btnClick)    local menu = cc.Menu:create()    config.bottomLayer:addChild(menu)    menu:setPosition(cc.p(0,0))    menu:addChild(btn)end

Registerscripthandler registers basic events

Registration of touch screen events

Function gamewindow: addlayertoucheventmethod2 () -- creates a single point touch screen event local listener = cc. eventlistenertouchonebyone: Create () -- register the touch screen start event listener: registerscripthandler (ontouchbegan, CC. handler. event_touch_began) -- register the listener: registerscripthandler (ontouchmoved, CC. handler. event_touch_moved) -- register the listener: registerscripthandler (ontouchended, CC. handler. event_touch_ended) -- Obtain the layer event dispatcher local eventdispatcher = config. bottomlayer: geteventdispatcher () -- The Event dispatcher registers a node event eventdispatcher: addeventlistenerwithscenegraphpriority (listener, config. bottomlayer) End



How to register a layer entry exit event

function gameWindow:addLayerEnterAndExitEvent()    local function onNodeEvent(eventType)        if eventType == "enter" then            log("enter")        elseif eventType == "exit" then            log("exit")        end    end    config.bottomLayer:registerScriptHandler(onNodeEvent)end


Cocos2dx + Lua registration event function details

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.