cocos2dx+lua 支持多點觸摸例子
測試環境:cocos2d-x-2.1.4
MultiTouchesTestLayer.lua代碼如下:
--[[
author: blog.163.com/mr_zyf/
]]
local layer=CCLayer:create()
--多點觸摸事件回調函數
local function onTouchsEvent(eventType,touchs)
--[[
eventType:觸摸事件類型.
touchs:多點觸摸的數組表,它的大小=n點觸摸*3
]]
if eventType=="began" then --手指開始觸摸屏幕
for i=1,#touchs,3 do
local x,y,id=touchs[i],touchs[i+1],touchs[i+2]--從touchs中獲取一點觸摸的座標和id
local sprite=CCSprite:create("CloseNormal.png")--創建精靈
sprite:setTag(id+100)--根據觸摸id設置精靈的標籤
sprite:setPosition(x,y)--根據觸摸座標設置精靈的位置
layer:addChild(sprite)--增加精靈到layer
end
elseif eventType=="moved" then --手指一直觸摸着屏幕移動
for i=1,#touchs,3 do
local x,y,id=touchs[i],touchs[i+1],touchs[i+2]
local sprite=layer:getChildByTag(id+100)--根據觸摸id,尋找精靈
sprite:setPosition(x,y)--根據觸摸座標設置精靈的位置
end
elseif eventType=="ended" then --手指一直觸摸着屏幕放開後
for i=1,#touchs,3 do
local id=touchs[i+2]--獲取觸摸id
layer:removeChildByTag(id+100,true)--根據觸摸id,從layer中刪除精靈
end
end
end
layer:setTouchEnabled(true)
layer:registerScriptTouchHandler(onTouchsEvent,true)--設置支持多點觸摸
return layer
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.