2012-08-07 41 views
0

這似乎很簡單,但我不知道如何監視持續觸摸。我想觸摸顯示屏或圖像,只要用戶沒有擡起手指繼續旋轉圖像。這裏是一個剪斷的代碼,我有:如何使用Corona SDK收聽連續觸摸事件

local rotate = function(event) 
if event.phase == "began" then 
    image1.rotation = image1.rotation + 1 
end 
return true 
end 

Runtime:addEventListener("touch", rotate)  

我希望發生旋轉,直到手指從屏幕上提起。 感謝您的任何建議。

回答

1

我最終這樣做了。如果您有更好的方法,請發佈您的答案!

local direction = 0 

function scene:move() 
crate.rotation = crate.rotation + direction 
end   

Runtime:addEventListener("enterFrame", scene.move)   

local function onButtonEvent(event) 
    if event.phase == "press" then 
    direction = 1 -- (-1 to reverse direction) 
    elseif event.phase == "moved" then 
    elseif event.phase == "release" then 
    direction = 0 
    end 
    return true 
end 

local button = widget.newButton{ 
    id = "rotate_button",  
    label = "Rotate", 
    font = "HelveticaNeue-Bold", 
    fontSize = 16, 
    yOffset = -2, 
    labelColor = { default={ 65 }, over={ 0 } }, 
    emboss = true, 
    onEvent = onButtonEvent 
} 
2

這個怎麼樣?

local crate = ... 
local handle 
local function rotate(event) 
    if event.phase == "began" and handle == nil then 
     function doRotate() 
      handle=transition.to(crate, 
       {delta=true, time=1000, rotation=360, onComplete=doRotate}) 
     end 
     doRotate() 
    elseif event.phase == "ended" and handle then 
     transition.cancel(handle) 
     handle = nil 
    end 
end 

Runtime:addEventListener("touch", rotate) 

這允許更好地控制旋轉速率。由於某些原因,如果您開始丟幀,依賴於enterFrame可能會有問題。

另外,手柄和非手柄的檢查是爲了適應多點觸摸。還有其他的方式(和更好的方法)來處理這個問題,但這是有利的(如果你不使用多點觸摸,應該無所謂)。