cocosLua node layer to add a click event

方式1:

local MainScene = class("MainScene", cc.load("mvc").ViewBase)

function MainScene:onCreate()
    -- add background image
    local layer = cc.Layer:create()
    layer:addTo(self)
    layer:addChild(display.newSprite("bg_0.jpg"):move(display.center))

    -- add HelloWorld label
    --local startBtn = cc.Label:createWithSystemFont("开始", "Arial", 20)
    local startBtn = cc.Sprite:create("GameScene_nn.png", cc.rect(760,0,250,80))
    startBtn:setScale(0.25,0.25)
    startBtn:move(display.center)
    layer:addChild(startBtn)

    --local tmpLayer = cc.Layer.create()
    local startLb = cc.Label:createWithSystemFont("开始", "Arial", 40)
    startBtn:addChild(startLb)
    startLb:setPosition(120,50)

    local function touchBegan(touch, event)
        --Node obtain registration event, here is the Layer 
        local the Node = Event: getCurrentTarget ()
         - Touch: getPreviousLocationInView (): obtain prior touch point coordinates of the location information UI 
        - getPreviousLocation (): OpenGL coordinates 
        - getLocationInView (): current coordinates 
        - getLocation (): 
        Print (Touch)
         return  false 
    End 
    local  function touchMoved (Touch, Event)
         return  false 
    End 
    local  function touchEnded (Touch, Event)
         return  false 
    End 
    local  function touchCanceled (Touch, Event)
         return  false
    End 
    
    local the listen = cc.EventListenerTouchOneByOne: Create () 
    the listen: registerScriptHandler (touchBegan, cc.Handler.EVENT_TOUCH_BEGAN) 
    the listen: registerScriptHandler (touchMoved, cc.Handler.EVENT_TOUCH_MOVED) 
    the listen: registerScriptHandler (touchEnded, cc.Handler.EVENT_TOUCH_ENDED) 
    the listen: registerScriptHandler (touchCanceled, cc.Handler.EVENT_TOUCH_CANCELLED) 

    local eventDispatcher = Layer: getEventDispatcher ()
     - added nodes order to add the listener priority mode, the upper node, the higher priority 
    - eventDispatcher: addEventListenerWithSceneGraphPriority (the listen, Layer ) 
    - void EventDispatcher :: addEventListenerWithFixedPriority (* EventListener listener, int fixedPriority) 
    -Add a fixed priority mode listener, fixedPriority> 0, the smaller the value, the higher the priority 
    eventDispatcher: addEventListenerWithFixedPriority (the listen, . 1 )
 End 
return MainScene 


mode 2: 

   local  function OnTouch (the eventType, X, Y)
         IF the eventType == " Began "  the then 
            return  to true 
        ELSEIF the eventType == " ended "  the then 
            return onTouchEnded (X, Y)
         End 
    End 

    Layer: setTouchEnabled ( to true ) 
    Layer: registerScriptTouchHandler (OnTouch) 


example: size change button click 

local MainScene = class("MainScene", cc.load("mvc").ViewBase)

function MainScene:onCreate()
    -- add background image
    local layer = cc.Layer:create()
    layer:addTo(self)
    layer:addChild(display.newSprite("bg_0.jpg"):move(display.center))

    -- add HelloWorld label
    --local startBtn = cc.Label:createWithSystemFont("开始", "Arial", 20)
    local startBtn = cc.Sprite:create("GameScene_nn.png", cc.rect(760,0,250,80))
    startBtn:setScale(0.25,0.25)
    startBtn:move(display.center)
    layer:addChild(startBtn)

    --local tmpLayer = cc.Layer.create()
    local startLb = cc.Label:createWithSystemFont("开始", "Arial", 40)
    startBtn:addChild(startLb)
    startLb:setPosition(120,50)

    local startBtn1 = cc.Sprite:create("GameScene_nn.png", cc.rect(760,0,250,80))
    startBtn1:setScale(0.25,0.25)
    startBtn1:setPosition(cc.p(200,100))
    --startBtn1:move(display.center)
    layer:addChild(startBtn1)

    local function touchBegan(touch, event)

        local node = event:getCurrentTarget()
        local location = node:convertToNodeSpace(touch:getLocation())
        local targetSize = node:getContentSize()
        local rect = cc.rect(0,0,targetSize.width, targetSize.height)
        if cc.rectContainsPoint(rect, location) then
            node:setScale(0.21,0.21)
            if node==startBtn then
                print("click button 1")
            else
                print("click button 2")
            end
        end
        return true
    end
    local function touchMoved(touch, event)
        print("touchMoved")
        return false
    end
    local function touchEnded(touch, event)
        local node = event:getCurrentTarget()
        node:setScale(0.25,0.25)
        return true
    end
    local function touchCanceled(touch, event)
        print("touchCanceled")
        return false
    end
    
    local listen = cc.EventListenerTouchOneByOne:create()
    listen:registerScriptHandler(touchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
    listen:registerScriptHandler(touchMoved,cc.Handler.EVENT_TOUCH_MOVED)
    listen:registerScriptHandler(touchEnded,cc.Handler.EVENT_TOUCH_ENDED)
    listen:registerScriptHandler(touchCanceled,cc.Handler.EVENT_TOUCH_CANCELLED)

    --local eventDispatcher = layer:getEventDispatcher()
    local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listen,startBtn)
    local listen1 = listen:clone()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listen1,startBtn1)


end
return MainScene

 

Guess you like

Origin www.cnblogs.com/zhangthree/p/11059353.html