1首先,lua中的触摸事件与cocos2d中的触摸事件相似,都需要添加监听者,都需要将监听者添加到触摸事件分发器中去
local listener=cc.EventListenerTouchOneByOne:create()
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
local dispacther=cc.Director:getInstance():getEventDispatcher()
eventdispacther:addEventListenerWithSceneGraphPriority(listener, self)
2触摸事件
function OnTouchBegan(touch,event)
return true
end
function OnTouchEnded(touch,event)
local p=touch:getLocation()
print(p.x)
print(p.y)
end
时间: 2024-10-29 04:39:56