Update the target coordinates while transitioning

泪湿孤枕 提交于 2019-12-11 01:18:35

问题


I am making a game in corona and I am facing a problem. I have a circle on screen and I want it to follow the touch coordinates continuously. I am using transition.to function to do so but the thing is, whenever this function get a coordinate, it completes the transition even if the coordinates are updated during the transition.

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

And in another function, I am doing this

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

The code works fine for simple touch but I want the circle to follow the touch even when it's moving.


回答1:


There are some examples which may solve your problem.

Refer:

1) Flight Path posted by carlos in corona Community.

2) Move Object through a path by renvis


Sample:

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)

Keep Coding.......... :)




回答2:


You cant add a new transition to an object, which already in a transition. Thats why you should cancel the older transition first. You can try :

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

Btw If you want to drag and drop objects, transitions are bad in performance way



来源:https://stackoverflow.com/questions/17692103/update-the-target-coordinates-while-transitioning

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!