前端之家收集整理的这篇文章主要介绍了
cocos2dx 事件监听,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
local btnSp=cc.Sprite:create("icon.png")
ly:addChild(btnSp)
btnSp:setPosition(600,100)
local function onTouchBegan(touch,event)
local locationInNode = btnSp:convertToNodeSpace(touch:getLocation())
local s = btnSp:getContentSize()
local rect = cc.rect(0,s.width,s.height)
if cc.rectContainsPoint(rect,locationInNode) then
print("------touch me--------")
return true
end
return false
end
local function onTouchMoved(touch,event)
print("----move----")
end
local function onTouchEnded(touch,event)
print("----end----")
end
local eventDispatcher = ly:getEventDispatcher()
local listener = cc.EventListenerTouchOneByOne:create()
listener:setSwallowTouches(true)
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
eventDispatcher:addEventListenerWithSceneGraPHPriority(listener,btnSp)