Corona中触控和水龙头之间的区别

时间:2013-01-13 13:24:50

标签: touch corona tap

我并不完全了解Corona中点击和触摸之间的区别。我使用它们,当一个触摸一个对象时都听了事件,直到我写了这段代码,当触摸nextButton时它会改变图像。这就像当我触摸nextButton时,它会调用该函数两次。但是,当我将其更改为点击时,它工作顺利。那么你能告诉我触摸和点击之间有什么区别吗?当我在这段代码中使用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)

2 个答案:

答案 0 :(得分: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制作的] {/ p>

    -- 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

答案 1 :(得分:1)

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

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

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

哪里有一个触摸处理程序,它完全相同:

本地函数touchHandler(事件)        如果event.phase ==“结束”那么             - 做东西        结束        返回true    端