2013-04-22 49 views
0

我有一個單詞搜索遊戲,有令牌,用戶可以使用它們來揭示他們必須找到的單詞。但我的問題是,我點擊了令牌之後,我無法突出顯示在搜索詞中找到的單詞,而是每次點擊時都會留下一個令牌,無論在哪個級別上。我試圖令牌: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) 

任何想法?

回答

2

嘗試修改你的函數是這樣的:

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) 
+0

我使用提供兩個例子,但現在wordfind熒光筆作品,但令牌按鈕不會:/ – 2013-04-22 11:08:08

+0

我相信我在功能的一個錯字名字..現在編輯 – 2013-04-22 11:30:04

+0

非常感謝你! – 2013-04-23 05:25:52

0

如果你想刪除你的監聽器,你必須在添加監聽器後添加刪除監聽器。

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)