cocos2dx 监听相关
local btm = ccui.Button:create("majia/btn_1.png","","majia/btn_2.png")
btm:setPosition(cc.p(200 + (i * 252), size.height / 2))
btm:addTouchEventListener(function(sender, eventType)
if eventType == 0 then -- ccui.TouchEventType.began
-- 开始按下状态 began
elseif eventType == 1 then -- ccui.TouchEventType.moved
-- 按着移动状态 鼠标没有放开 - moved
elseif eventType == 2 then -- ccui.TouchEventType.ended
-- 按下后在空间内弹开左键状态 --ended
elseif eventType == 3 then -- ccui.TouchEventType.canceled
--取消状态 canceled
end
end)
全局监听
local function onTouchEnded( touch, event )
cclog("onTouchEnded")
end
cclog("onTouchEnded")
end
local function onTouchBegan( touch, event )
--获得当前点击位置坐标 touch:getLocation().x , touch:getLocation().y
--获得当前点击位置坐标 touch:getLocation().x , touch:getLocation().y
return true -- 不加这个后面三个监听函数默认不执行
end
end
local function onTouchMoved( touch, event )
cclog("onTouchMoved")
end
local listener1 = cc.EventListenerTouchOneByOne:create() --创建一个单点事件监听
listener1:setSwallowTouches(true) --是否向下传递
listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, self) --分发监听事件
cclog("onTouchMoved")
end
local listener1 = cc.EventListenerTouchOneByOne:create() --创建一个单点事件监听
listener1:setSwallowTouches(true) --是否向下传递
listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, self) --分发监听事件