在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

有人能告诉我做这件事的正确方法是什么以及我做错了什么吗。

编辑

这是我的第一个图像事件处理程序。我已经尝试过添加dice: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

这是我的旋转医疗功能

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

运行时触摸处理程序覆盖整个屏幕。如果你想在物体上发生触摸事件,你通常会将触摸事件直接添加到该物体上:

someImage:addEventListener("触摸",myTouchFunction)

然后,我就不用担心添加和删除侦听器,而是设置一些标志,指示是否允许触摸。

最新更新