【问题标题】:Difference between touch and tap in CoronaCorona中触摸和点击之间的区别
【发布时间】:2013-01-13 13:24:50
【问题描述】:

我不太明白 Corona 中的点击和触摸之间有什么区别。我同时使用它们,当一个对象触摸一个对象时,它们都会监听事件,直到我编写了这段代码,它会在触摸 nextButton 时更改图像。就像当我触摸 nextButton 时,它调用了两次函数。但是,当我将其更改为 Tap 时,它运行顺利。那么你能告诉我 touch 和 tap 有什么区别,以及我在这段代码中使用 touch 时造成问题的原因是什么?

 function nextButton:touch(event)
   if i==7 then 
   else
    i=i+1   
    imageObject:removeSelf()    
    imageObject =display.newImage(imageTable[i]..".jpg")
    imageObject.x=_W/2
    imageObject.y=_H/2 
   end
 end

 nextButton:addEventListener("touch", nextButton)

【问题讨论】:

    标签: touch coronasdk tap


    【解决方案1】:

    在电晕触摸监听器中,您有 3 个状态:

       function listener(event)
    
         if event.phase == "began" then
       -- in the 1st tap the phase will be "began"
           elseif event.phase == "moved" then
       -- after began phase when the listener will be called with moved phase,like          touched coordinates
           elseif event.phase == "ended" then
           --when the touch will end 
         end
    
    
       end
    
        nextButton:addEventListener("touch", listener)
    

    --[[ 这是一个简单的图像触摸监听器,自制按钮等,顺便说一句,当你需要你的按钮时,使用 ui 库正是为此制作的 http://developer.coronalabs.com/code/enhanced-ui-library-uilua ]]

        -- example for usage
        local ui = require "ui" -- copy the ui.lua to your apps root directory
    
       yourButton = ui.newButton{
        defaultSrc = "menu/icon-back.png",--unpressed state image 
        x=85,
            y=display.contentHeight-50,
        defaultX = 110,
        defaultY =80,
        offset=-5,
    
    
        overSrc = "menu/icon-back-2.png",--pressed state image
        overX = 110,
        overY = 80,
        onEvent = buttonhandler,
        id = "yourBtnId" -- what you want
        }
    
          local function buttonhandler(event)
    
             if event.phase == "release" then
    
              --if you have more buttons handle it whit they id-s
                if event.id == "yourBtnId" then
                  -- when your button was finally released (like in 1st example ended, but this will be called only if the release target is your button)
                end 
    
             end
          end
    

    【讨论】:

      【解决方案2】:

      “点击”是一种短暂的触摸和释放动作。触摸可以是触摸、移动然后释放或触摸并按住等。点击事件简化了您的代码,因为您只得到一个事件:点击发生。您不必为所有的触摸状态编写代码。

      典型的点击处理程序如下所示:

      local function tapHandler(event)
          -- do stuff
          return true
      end
      

      哪里有一个触摸处理程序,它会做完全相同的事情:

      本地函数 touchHandler(event) 如果 event.phase == "结束" 那么 - 做东西 结尾 返回真 结束

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-20
        • 2014-01-07
        • 2011-12-24
        • 1970-01-01
        • 2020-11-22
        • 2014-09-21
        • 2020-05-01
        相关资源
        最近更新 更多