转换时更新目标坐标

时间:2013-07-17 05:53:37

标签: lua corona

我正在日冕制作游戏,我遇到了一个问题。我在屏幕上有一个圆圈,我希望它能够连续跟踪触摸坐标。我正在使用transition.to函数来做到这一点,但事实是,每当这个函数获得一个坐标时,即使在转换过程中更新了坐标,它也会完成转换。

if event.phase == "began" or event.phase == "moved" then
    follow = true
    touchX = event.x; touchY = event.y
elseif event.phase == "ended" then
    follow = false
end

在另一个功能中,我正在做这个

if follow == true then
    transition.to(circle, {time = 500, x = touchX, y = touchY, transition = easing.inOutQuad})
end

该代码适用于简单的触摸,但我希望圆圈即使在移动时也能跟随触摸。

2 个答案:

答案 0 :(得分:1)

有一些例子可以解决你的问题。

参考:

1)Flight Path由卡洛斯在电晕社区发布。

2)Move Object through a path由renvis


样品:

local circle = display.newCircle(10,10,20)
circle.x = 160
circle.y = 160

local olderTransition
local function moveCircle(e)
  if olderTransition ~= nil then
    transition.cancel( olderTransition )
  end
  olderTransition = transition.to(circle,{time=100,x=e.x,y=e.y})
end
Runtime:addEventListener("touch",moveCircle)

保持编码..........:)

答案 1 :(得分:1)

您无法向已在转换中的对象添加新转换。这就是为什么你应该首先取消旧的过渡。你可以尝试:

local olderTransition -- This should be visible outside of your function
local function blabla()
    if follow == true then
        if olderTransition ~= nil then
            transition.cancel( olderTransition )
        end
        olderTransition = transition.to(circle, {time = 500, x = touchX, y = touchY, transition = easing.inOutQuad, onComplete = function() olderTransition = nil end })
    end
end

顺便说一下如果你想拖放对象,那么转换在性能方面是不好的