2013-12-08 187 views
0

我想在這裏實現的是...如果我觸摸一個圖像它被禁用。那麼我想通過觸摸另一個圖像來啓用它。但禁用後我不能重新啓用它。 我爲第一圖像的觸摸事件,我稱之爲使用刪除,然後再添加運行時事件監聽器corona

Runtime:addEventListener("touch", diceFunction2) 

,我又把它使用

Runtime:removeEventListener("touch", diceFunction2) 

刪除,但我想它添加再次使用該觸摸事件處理

local function guti11touched(event) 
    if event.phase == "began" then 
    if playerTurn%2==0 then 
     if rand == 6 and gutiStatus.guti11.status == false then 
      gutiStatus.guti11.status = true 
      gutiStatus.guti11.count = 1 
      local i = gutiStatus.guti11.count 
      transition.moveTo(guti11, {x=background.x+player1Sequence[i][1],y=background.y-player1Sequence[i][2]}) 
     elseif gutiStatus.guti11.status == true and gutiStatus.guti11.count+rand <= 57 then 
      gutiStatus.guti11.count = gutiStatus.guti11.count + rand 
      local i = gutiStatus.guti11.count 
      transition.moveTo(guti11, {x=background.x+player1Sequence[i][1],y=background.y-player1Sequence[i][2]}) 
      if i == 57 then 
       guti11.isClickable = false 
      end 
     end 
    end 
    elseif event.phase == "ended" then 
    Runtime:addEventListener("touch", diceFunction2) 
    end 
return true 
end 

有人可以告訴我什麼是正確的方式來做到這一點,我做錯了什麼。

編輯

這是我的第一個圖像事件處理程序。我已經嘗試添加骰子:addEventListener(「touch」,diceFunction2),但在這種情況下,圖像只是繼續旋轉,永遠不會停止,因爲我隨機選擇圖像,所以我可以做到代替運行時?

--for rotating dice 
local function diceFunction2(event) 

if event.phase == "began" then 
     local laserChannel = audio.play(laserSound) 
     rand=math.random(6) 
     dice = display.newImage("images/dice3droll.png") 
     dice.x = 75 
     dice.y = 480 
     dice:scale(1/scale_factor,1/scale_factor) 
     display.getCurrentStage():setFocus(dice) 
     dice.isFocus = true 
     Runtime:addEventListener("enterFrame", rotateDice) 
    elseif dice.isFocus then    
     if event.phase == "moved" then 
     elseif event.phase == "ended" then 
      if rand == 1 then 
       Runtime:removeEventListener("enterFrame", rotateDice) 
       dice:removeSelf() 
       local dice1 = display.newImage("images/ek.png") 
       dice1.x = 75 
       dice1.y = 480 
       dice1:addEventListener("touch", diceFunction2) 
       display.getCurrentStage():setFocus(nil) 
       dice.isFocus = false 


      elseif rand == 2 then 
       Runtime:removeEventListener("enterFrame", rotateDice) 
       dice:removeSelf() 
       local dice2 = display.newImage("images/dui.png") 
       dice2.x = 75 
       dice2.y = 480 
       display.getCurrentStage():setFocus(nil) 
       dice.isFocus = false  


      elseif rand == 3 then 
       Runtime:removeEventListener("enterFrame", rotateDice) 
       dice:removeSelf() 
       local dice3 = display.newImage("images/tin.png") 
       dice3.x = 75 
       dice3.y = 480 
       display.getCurrentStage():setFocus(nil) 
       dice.isFocus = false  

      elseif rand == 4 then 
       Runtime:removeEventListener("enterFrame", rotateDice) 
       dice:removeSelf() 
       local dice4 = display.newImage("images/charr.png") 
       dice4.x = 75 
       dice4.y = 480 
       display.getCurrentStage():setFocus(nil) 
       dice.isFocus = false 


      elseif rand == 5 then 
       Runtime:removeEventListener("enterFrame", rotateDice) 
       dice:removeSelf() 
       local dice5 = display.newImage("images/pach.png") 
       dice5.x = 75 
       dice5.y = 480 
       display.getCurrentStage():setFocus(nil) 
       dice.isFocus = false  


      elseif rand == 6 then 
       Runtime:removeEventListener("enterFrame", rotateDice) 
       dice:removeSelf() 
       local dice6 = display.newImage("images/chokka.png") 
       dice6.x = 75 
       dice6.y = 480 
       display.getCurrentStage():setFocus(nil) 
       dice.isFocus = false   
      end 

     end 
end 
end 

這是我的rotatedice功能

local function rotateDice() 
    dice.rotation = dice.rotation + 200 
end 

回答

1

運行時的觸摸處理覆蓋整個屏幕。如果你想在對象上的觸摸事件,你通常直接添加觸摸事件給該對象:

someImage:的addEventListener(「觸摸」,myTouchFunction)

然後,我就不會擔心添加和刪除監聽器而是設置一些標誌來指示觸摸是否被允許發生。

+0

在我的情況下,我不使用特定圖像,當我點擊一個圖像時,它隨機更改爲另一圖像。所以我不知道如果我可以使用someImage:addEventListener(「觸摸」,myTouchFunction) – Shuvro

+0

我已更新我的問題與功能,請看看 – Shuvro

+0

我添加觸摸事件的對象和設置標誌,但我不得不修改我的代碼很多,但最終它的工作。感謝您的意見 – Shuvro