2013-07-23 66 views
0

[求助]感谢您的期待,但我想通了。我需要在我的某些条件下取消嵌套return true声明。Lua Corona SDK冲突事件监听器

本周我刚刚开始学习Lua,并开始使用Corona SDK编写2D横向卷轴游戏。在这个游戏中,玩家的角色通过按下屏幕上显示的按钮来移动,就像虚拟游戏板一样。这些按钮的工作很好,但问题是,我有一个

Runtime:addEventListener("tap", onScreenTap) 

事件侦听器,然后调用拍摄()函数,只要水龙头注册到从播放器火炮弹。这会导致每次从其中一个移动按钮上抬起触摸时发射弹丸。

当我完成触摸某个移动键时,是否有任何方法可以阻止拍摄功能拨打电话?我曾尝试

display.getCurrentStage:setFocus() 

,并把

return true 

在运动功能的结束,但似乎没有任何工作。

回答

2

您可以在您拥有的每个触控功能中使用此基础知识。或者仅限于此,适用于所有触摸事件。将触摸事件组合在单个功能中可以解决您的问题。

function inBounds(event) 
    local bounds = event.target.contentBounds 
    if event.x > bounds.xMin and event.x < bounds.xMax and event.y > bounds.yMin and event.y < bounds.yMax then 
     return true 
    end 
    return false 
end 


function touchHandler(event) 
    if event.phase == "began" then 
     -- Do stuff here -- 
     display.getCurrentStage():setFocus(event.target) 
     event.target.isFocus=true 
    elseif event.target.isFocus == true then 
     if event.phase == "moved" then 
      if inBounds(event) then 
       -- Do stuff here -- 
      else 
       -- Do stuff here -- 
       display.getCurrentStage():setFocus(nil) 
       event.target.isFocus=false 
      end 
     elseif event.phase == "ended" then 
      -- Do stuff here -- 
      display.getCurrentStage():setFocus(nil) 
      event.target.isFocus=false 
     end 
    end 
    return true 
end 

顺便说一句,如果你尝试在运行时使用这个,它会抛出和错误。你可以添加一个事件监听器到后台,或者只是把一些控制机制,如

if event.target then 
-- blah blah 
end 
+0

感谢您的回复Dogancan,我可以看到这个语法如何是非常有用的。但是,我解决了我遇到的问题。 –