touch - Corona 中触摸和点击的区别

标签 touch coronasdk tap

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

 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)

最佳答案

在 corona touch 监听器中,您有 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

关于touch - Corona 中触摸和点击的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14303936/

相关文章:

.net - 为什么 Task.WhenAll 上的等待不抛出 AggregateException?

html - 如何使按钮保持事件状态而不是恢复到正常状态?

iOS 触摸阻塞问题

c# - Unity3D,3D 和 UI Raycaster 是否同时响应触摸?

lua - Corona SDK 检测物体移除

android - 电晕SDK : Director Class and Grouping objects

button - 声音和音乐Corona SDK的2个开关按钮?

ios - 点击更改 UIImageView alpha 状态

ios - 如何找出触摸事件在哪个 View 结束?

sockets - 连接到哪个物理接口(interface) tun/tap 设备