2

我不完全明白 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)
4

2 回答 2

3

在电晕触摸监听器中,您有 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
于 2013-01-14T08:06:25.170 回答
1

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

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

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

where 有一个执行完全相同的操作的触摸处理程序如下所示:

local function touchHandler(event) if event.phase == "ended" then -- do stuff end return true end

于 2013-01-20T22:05:48.833 回答