同时触摸多个button,只想要响应其中一个button的事件应该怎么做,求助。
local function touchEvent(sender,eventType)
local name = sender:getName()
if eventType == ccui.TouchEventType.began then
if name == "1" then
print("1")
elseif name == "2" then
print("2")
end
end
end
local textButton1 = ccui.Button:create()
textButton1:setName("1")
textButton1:setTouchEnabled(true)
textButton1:setScale9Enabled(true)
textButton1:loadTextures("cocosui/button.png", "cocosui/buttonHighlighted.png", "")
textButton1:setContentSize(cc.size(180, textButton:getVirtualRendererSize().height * 1.5))
textButton1:setTitleText("Text Button scale9 render")
textButton1:setPosition(cc.p(widgetSize.width / 2.0, widgetSize.height / 2.0))
textButton1:addTouchEventListener(touchEvent)
local textButton2 = ccui.Button:create()
textButton2:setName("2")
textButton2:setTouchEnabled(true)
textButton2:setScale9Enabled(true)
textButton2:loadTextures("cocosui/button.png", "cocosui/buttonHighlighted.png", "")
textButton2:setContentSize(cc.size(180, textButton:getVirtualRendererSize().height * 1.5))
textButton2:setTitleText("Text Button scale9 render")
textButton2:setPosition(cc.p(widgetSize.width / 2.0, widgetSize.height / 2.0))
textButton2:addTouchEventListener(touchEvent)
直接使用test的代码来做测试,两个button同时触摸,都会相应,如果需要只响应一个的话,不知道应该怎么处理。