2013-01-13 67 views
2

我不完全理解電暈中水龍頭和觸摸之間的區別。我使用它們,當一個對象觸及一個對象時,都會聽到事件,直到我寫下這段代碼,當nextButton被觸摸時這些代碼會改變圖像。就像我觸摸nextButton時,它會調用該函數兩次。但是,當我改變它來點擊時,它工作順利。那麼你能告訴我觸摸和點擊之間有什麼區別,以及當我在這段代碼中使用觸摸時會產生什麼麻煩?電暈中的觸摸和水龍頭之間的區別

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) 

回答

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) 

- [[ 這是對圖像的簡單的觸摸監聽器,自制按鈕等,順便說一句,當你需要你要跟按鈕,使用究竟是什麼做的這個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 
1

A「龍頭」的UI庫是一個短暫觸摸和釋放動作。觸摸可以是觸摸,移動然後釋放,也可以是觸摸並按住等等。輕擊事件會簡化您的代碼,因爲您只有一個事件發生:輕拍發生。您不必爲所有觸摸狀態編碼。

一個典型的自來水處理程序看起來像:

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

,其中有一個觸摸處理程序,做同樣的事情會是這樣的:

本地函數touchHandler(事件) 如果event.phase = =「ended」then - do stuff end return true end