zoukankan      html  css  js  c++  java
  • cocosLua节点层添加点击事件

    方式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)
            --取得注册事件的节点,这里是layer
            local node = event:getCurrentTarget()
            --touch:getPreviousLocationInView():取得之前触摸点的位置信息 UI坐标
            --getPreviousLocation() :OpenGL坐标
            --getLocationInView():当前坐标
            --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 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()
        --节点添加的先后顺序为优先级的添加监听器方式,节点越上层,优先级越高
        --eventDispatcher:addEventListenerWithSceneGraphPriority(listen,layer)
        --void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
        --固定优先级的添加监听器方式,fixedPriority>0, 数值越小,优先级越高
        eventDispatcher:addEventListenerWithFixedPriority(listen, 1)
    end
    return MainScene
    
    
    方式2:
    
       local function onTouch(eventType, x, y)
            if eventType == "began" then
                return true
            elseif eventType == "ended" then
                return onTouchEnded(x, y)
            end
        end
    
        layer:setTouchEnabled(true)
        layer:registerScriptTouchHandler(onTouch)
    
    
    示例: 按钮点击后大小变化
    
    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
  • 相关阅读:
    p1297
    p2023
    p1612
    逆元总结
    p1652
    考试总结10-08
    p1459
    p1821
    p1863
    p1884
  • 原文地址:https://www.cnblogs.com/zhangthree/p/11059353.html
Copyright © 2011-2022 走看看