如何使用Corona SDK监听连续的触摸事件

这似乎很简单,但我不知道如何监测持续的触摸。我想要触摸屏幕或图像,只要用户没有抬起手指就继续旋转图像。这是我目前的代码:

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

Runtime:addEventListener("touch", rotate)

我想要旋转动作持续到手指离开屏幕。谢谢任何建议。

点赞
用户504534
用户504534

我最终用了这种方法。如果您有更好的方法,请发布您的答案!!

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 = "旋转",
  font = "HelveticaNeue-Bold",
  fontSize = 16,
  yOffset = -2,
  labelColor = { default={ 65 }, over={ 0 } },
  emboss = true,
  onEvent = onButtonEvent
}
2012-08-07 16:49:28
用户932927
用户932927
怎么样?

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可能会有问题。

另外,对handle和not handle进行的检查是为了适应多点触控。还有其他方法(和更好的方法)来处理这个问题,但这是一个方便的方法(如果不使用多点触控,则完全不会有影响)。

2012-08-07 20:14:17