Modify the time value of a transition

2019-08-27 09:25发布

问题:

How can i change the time value of a transition while is executing? For example I have the object "cesta" moving from the left to the right with a time of 4000ms then for some reason I want to change the time value to move it faster.

function createCesta()
    ...
    transition.to(cesta, {time = 4000, x = screenW + 110})
    ...
end


function touchScreen(event)   
    if event.phase == "began" then
    end
    if event.phase == "ended" then
        --change the time value from here "from 4000 to 2000"
    end
end

回答1:

The docs at http://docs.coronalabs.com/api/library/transition/index.html indicate that there is no function call to do this. You would therefore have to cancel the current incomplete transition and create a new one. For example,

local trans
local transTime = 4000 -- ms
local transStart
local object

function someEventHandler(event)
   transition.cancel(trans)
   local remaining = system.getTimer() - transStart - transTime 
   if remaining > 0 then
       trans = transition.to(object, { time = remaining/2, x = ... }
   end
end

function spawn()
   object = display.newText(...)
   trans = transition.to(object, {time = transTime}
   transStart = system.getTimer()
end

This shows a spawn function where you create a display object and make it move via transition to some x, and an event handler that will get called at some point. It computes how much time is left in the transition and if > 0, creates a new transition with half that remaining time so double the "motion" speed of x.