【问题标题】:touch event not being removed, keeps working no matter where I touch the screen触摸事件没有被删除,无论我在哪里触摸屏幕都可以继续工作
【发布时间】:2013-04-22 19:30:40
【问题描述】:

我有一个单词搜索游戏,其中包含令牌,用户可以使用它们来显示他们必须找到的单词。但我的问题是,在我单击标记后,我无法突出显示在单词搜索中找到的单词,而是每次单击时都会带走标记,无论在关卡的哪个位置。我试过 token: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)

有什么想法吗?

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    尝试像这样修改你的函数:

    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 )
    

    【讨论】:

    • 我使用了提供的两个示例,但现在 wordfind 荧光笔可以工作,但令牌按钮不能:/
    • 我相信我的函数名称有错字。现在已编辑
    【解决方案2】:

    如果你想删除你的监听器。你必须在添加监听器之后添加删除监听器。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 2012-06-30
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多