简体   繁体   中英

How to Listen to continuous touch events with Corona SDK

This seemed simple to do but I do not know how to monitor for continued touching. I want to touch the display or an image and as long as the user has not lifted his finger continue to rotate an Image. Here is a snip the code I have:

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

Runtime:addEventListener("touch", rotate)    

I want the rotation to occur until the finger is lifted from the screen. Thanks for any advice.

How about this?

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) 

This allows for better control of the rate of rotation. Relying on enterFrame might be problematic if you start dropping frames for some reason.

Also, the checks for handle and not handle are to accomodate multi touch. There are other ways (and better ways) to handle this but it's expedient (and shouldn't matter at all if you aren't using multitouch.)

I ended up doing this. Please post your answer if you have a better method !!

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
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM