过渡时更新目标坐标。

我正在使用 corona 制作一个游戏,遇到了问题。屏幕上有一个圆,我想让它持续地跟随触摸坐标。我使用 transition.to 函数来做到这一点,但问题是,无论在过渡期间坐标是否更新,每当此函数得到一个坐标,它都会完成过渡。

如果事件.phase == "开始" or 事件.phase == "移动" then
    follow = true
    touchX = 事件.x; touchY = 事件.y
elseif 事件.phase == "结束" then
    follow = false
end

在另一个函数中,我正在做这个:

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

对于简单触摸,代码可以正常工作,但我希望圆可以在移动时跟随触摸。

点赞
用户1979583
用户1979583

下面有一些例子可能会解决你的问题。

参考:

  1. Flight Path,由corona社区的carlos发布。

  2. 通过路径移动对象,由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)

继续编码...... :)

2013-07-17 09:43:28
用户2186639
用户2186639

无法给一个对象添加一个新的过渡动画,当它已经在一个过渡动画中时。那就是为什么你应该先取消旧的过渡动画。你可以尝试:

local olderTransition -- 这应该在你的函数外面可见
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

顺便说一句,如果你想拖放对象,过渡动画在性能上不可取。

2013-07-17 09:47:13