Cocos2dx-lua触摸事件处理

来源:互联网 发布:东方网络收购会成功吗 编辑:程序博客网 时间:2024/05/21 20:24

方式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)--取得注册事件的节点,这里是layerlocal node = event:getCurrentTarget()--touch:getPreviousLocationInView():取得之前触摸点的位置信息 UI坐标--getPreviousLocation() :OpenGL坐标--getLocationInView():当前坐标--getLocation():print(touch)return falseendlocal function touchMoved(touch, event)return falseendlocal function touchEnded(touch, event)return falseendlocal function touchCanceled(touch, event)return falseend        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)endreturn 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) thennode:setScale(0.21,0.21)if node==startBtn thenprint("click button 1")elseprint("click button 2")endendreturn trueendlocal function touchMoved(touch, event)print("touchMoved")return falseendlocal function touchEnded(touch, event)local node = event:getCurrentTarget()node:setScale(0.25,0.25)return trueendlocal function touchCanceled(touch, event)print("touchCanceled")return falseend        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)endreturn MainScene









原创粉丝点击