触摸事件没有被删除,无论我在哪里触摸屏幕,都可以继续工作
问题描述:
我有一个单词搜索游戏,有令牌,用户可以使用它们来揭示他们必须找到的单词。但我的问题是,我点击了令牌之后,我无法突出显示在搜索词中找到的单词,而是每次点击时都会留下一个令牌,无论在哪个级别上。我试图令牌:removeEventListener但没有工作,也许我把它放在了错误的地方触摸事件没有被删除,无论我在哪里触摸屏幕,都可以继续工作
function token:touch(event)
if event.phase == "began" then
if storyboard.state.score >0 then
storyboard.state.score = storyboard.state.score - 1
score.text = tostring(storyboard.state.score)
clueText.isVisible = false
answerText.isVisible = true
display.getCurrentStage():setFocus(event.target)
event.target.isFocus = true
elseif event.target.isFocus then
if event.phase == "moved" then
print("user has moved their finger off the token.")
elseif event.phase == "ended" then
print("user has used a token")
display.getCurrentStage():setFocus(nil)
event.target.isFocus = false
end
end
return true
end
end
menubutton:addEventListener("touch", menubutton)
token:addEventListener("touch", token)
任何想法?
答
尝试修改你的函数是这样的:
function tokenTouch(event)
if event.phase == "began" then
if storyboard.state.score >0 then
storyboard.state.score = storyboard.state.score - 1
score.text = tostring(storyboard.state.score)
clueText.isVisible = false
answerText.isVisible = true
display.getCurrentStage():setFocus(event.target)
event.target.isFocus = true
end
elseif event.target.isFocus then
if event.phase == "moved" then
print("user has moved their finger off the token.")
elseif event.phase == "ended" then
print("user has used a token")
display.getCurrentStage():setFocus(nil)
event.target.isFocus = false
end
end
return true
end
token:addEventListener("touch", tokenTouch)
答
如果你想删除你的监听器,你必须在添加监听器后添加删除监听器。
local function token(event)
if event.phase == "began" then
elseif event.phase == "moved" then
elseif event.phase == "ended" then
end
return true
end
token:addEventListener("touch",token)
token:removeEventListener("touch",token)
我使用提供两个例子,但现在wordfind荧光笔作品,但令牌按钮不会:/ – 2013-04-22 11:08:08
我相信我在功能的一个错字名字..现在编辑 – 2013-04-22 11:30:04
非常感谢你! – 2013-04-23 05:25:52