【问题标题】:Remove and then adding runtime event listener again in corona删除然后在电晕中再次添加运行时事件侦听器
【发布时间】:2013-12-08 22:25:01
【问题描述】:

我在这里想要实现的是……如果我触摸一张图像,它就会被禁用。然后我想通过触摸另一个图像来启用它。但禁用后我无法重新启用它。 我有一个触摸事件用于我调用的第一张图片

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 

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

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

    someImage:addEventListener("touch", myTouchFunction)

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

    【讨论】:

    • 在我的情况下,我没有使用特定图像,当我单击一个图像时,它会随机更改为另一个图像。所以我不确定我是否可以使用 someImage:addEventListener("touch", myTouchFunction)
    • 我已经用函数更新了我的问题,请看一下
    • 我将触摸事件添加到对象并设置标志,但我不得不修改我的代码很多,但最后它工作了。谢谢你的建议
    • 我建议为此使用精灵。您将有一个显示对象,它有 6 个帧,每个骰子面一个。然后,您只需更改显示的帧。
    • 好的,再次感谢...我也会尝试使用精灵来做到这一点...感谢您的建议..如果我成功了,我会告诉你的。 :D
    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2018-09-04
    • 2013-06-27
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多