Cocos2dx-lua觸摸事件處理

方式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









發佈了35 篇原創文章 · 獲贊 15 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章