在电晕中再次删除然后再添加运行时事件侦听器

时间:2013-12-08 22:25:02

标签: lua 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

这是我的旋转功能

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

1 个答案:

答案 0 :(得分:1)

运行时触摸处理程序覆盖整个屏幕。如果要在对象上进行触摸事件,通常可以将触摸事件直接添加到该对象:

someImage:addEventListener(“touch”,myTouchFunction)

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