【问题标题】:Corona SDK: touch eventCorona SDK:触摸事件
【发布时间】:2012-02-26 19:19:06
【问题描述】:

如何检测屏幕是否被触摸?在我看来,触摸屏幕且手指不移动时不会生成触摸事件。

【问题讨论】:

    标签: sdk lua touch coronasdk


    【解决方案1】:

    是的,只记录手指移动的变化。放下手指,抬起手指并拖动触发事件。

    【讨论】:

      【解决方案2】:

      但是,你可以这样做

       e.phase == "began"
      

      在您的事件函数中。这将在用户将手指放在屏幕上时触发。

      【讨论】:

        【解决方案3】:

        触摸事件是分阶段处理的。所以由触摸产生的事件有“开始”、“移动”、“结束”和“取消”阶段。因此,您可以通过以下方式使用检测:

        self.isTouched = false;
        
        function defaultTouchHandler(e)
            if(e.phase == "began") then
                print("Tapped")
                self.isTouched = true;
                --User has touched the screen (not moving). Do "onMouseDown" things here
            elseif(e.phase == "moved") then
                print("Moved")
                --User is moving their finger wile touching. Do "onMouseMoved" things here
            elseif(e.phase == "cancelled" or e.phase == "ended") then
                print("End of touch")
                self.isTouched = false;
                --User lifted their finger, or an interrupt happened. Do "onMouseUp" things here
            end
        end
        
        self:addEventListener("touch", defaultTouchHandler)
        

        当您需要检查屏幕是否被触摸时,只需执行以下操作:

        if(isTouched) then
            --Screen is being touched
        else
            --Screen is not being touched
        end
        

        编辑:显然,您可以将addEventListener 行上的“self”更改为您希望在其上监听触摸事件的任何对象

        【讨论】:

          【解决方案4】:
          local object = display.newImage( "ball.png" )
          object.id = "ball object"
          
           local function onObjectTouch( event )
          if ( event.phase == "began" ) then
              print( "Touch event began on: " .. event.target.id )
          elseif ( event.phase == "ended" ) then
              print( "Touch event ended on: " .. event.target.id )
          end
          return true
          end
          object:addEventListener( "touch", onObjectTouch )
          

          【讨论】:

            猜你喜欢
            • 2013-04-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-17
            • 1970-01-01
            • 2014-01-07
            • 2015-04-22
            • 1970-01-01
            相关资源
            最近更新 更多