Completing a Transition instantly

Hi guys,

I notice that ‘transition.cancel’ will stop any transition and leave it hanging until a resume is called.

Is there a way to ‘end’ a transition instantly, where the objects just straight away go to its final transition phase/location ? This will be useful in a scene with multiple-transition with a lot of user interaction.

thanks

Do you want the transition to end when a user provides an input, or do you want it to end randomly, or on a timer? You could create a function that cancels the transition in question when each is fired. Something like this, for a touch input:

local trans1 local uiBox = display.newRect(0,0,32,32) local function finishTrans1() uiBox.x = display.contentWidth/4 uiBox.y = display.contentCenterY end local function playerInit(event) if event.phase == "ended" then transition.cancel(trans1) uiBox.x = display.contentWidth/4 uiBox.y = display.contentCenterY uiBox.xScale = 2 uiBox.yScale = 2 end end trans1 = transition.to(uiBox, {time=5000, alpha=.1, xScale=10, yScale=10, onComplete=finishTrans1}) Runtime:addEventListener("touch", playerInit)

Dear Panc,

Thanks. I did think of it, but doing it that way will be double job. Furthermore i have multiple transitions.

I was hoping for a function (maybe like transition.finish) to bring all the transition to an end instantly. 

I’m not sure what stimulus you are looking for, that would signal all transitions should be completed. You could modify the above to relate to a timer, or for something specific in the app. You’d have to code it yourself of course. 

Can you describe a bit more about your issue? Did you want to identify all transitions in the same “group”? What app behavior do you want to listen for, to signal that all transitions should be cancelled and completion be acted on?

Hi,

Could you use onCancel events and just use transition.cancel without parameters to cancel all ongoing transitions, something like:

[lua]

local function touchEvent(event)
    if event.phase==“began” then
        transition.cancel()
    end
end
local circle = display.newCircle(display.contentCenterX, display.contentCenterY,20)
local trans = transition.to(circle,{time=10000, x=0, y=0, xScale = 2,yScale = 2, onCancel = function(obj)
    obj.x = 0
    obj.y = 0
    obj.xScale = 2
    obj.yScale = 2
end})

local circle2 = display.newCircle(display.contentCenterX, display.contentCenterY,20)
local trans2 = transition.to(circle2,{time=10000, x=display.contentWidth, y=display.contentHeight, xScale = 4,yScale = 4, onCancel = function(obj)
    obj.x = display.contentWidth
    obj.y = display.contentHeight
    obj.xScale = 4
    obj.yScale = 4
end})

circle:addEventListener(“touch”, touchEvent)
circle2:addEventListener(“touch”, touchEvent)

[/lua]

local trans=transition.to( obj,{time=3000,x=320}

when you cancel the trans.
trans._target is the obj
trans._keysFinish is the params
so you can do
trans._target.x=trans._keysFinish.x
trans._target.y=trans._keysFinish.y
and so on for the other values
the onComplete function is
trans._onComplete()

So I suppose something like this could be used to complete all ongoing transitions to their final states when transitions are canceled:

[lua]

local function completeTransition(obj)
    if obj.trans then
        for key,value in pairs(obj.trans._keysFinish) do
            obj[key] = value
        end
        obj.trans = nil
    end
end

local function touchEvent(event)
    if event.phase==“began” then
        transition.cancel()
    end
end

local circle = display.newCircle(display.contentCenterX, display.contentCenterY,20)
circle.alpha = 0
local trans = transition.to(circle,{time=10000, x=0, y=0, xScale = 2,yScale = 2, alpha = 1, onCancel = function(obj) completeTransition(obj) end })
circle.trans = trans

local circle2 = display.newCircle(display.contentCenterX, display.contentCenterY,20)
local trans2 = transition.to(circle2,{time=10000, x=display.contentWidth, xScale = 4, yScale = 4, onCancel = function(obj) completeTransition(obj) end})
circle2.trans = trans2

circle:addEventListener(“touch”, touchEvent)
circle2:addEventListener(“touch”, touchEvent)

[/lua]

When you touch either of the circles all transitions are canceled and all transition properties are set to their final values. This approach requires the transition to be stored in the displayobject and could of course be handled a bit differently.

looks bout right

Thanks all for the insights and suggestions. It seems that the only way is to do a ‘double job’, where a separate function is called to ‘finalize the object in transition’. I’ll put it into the Corona request feature. Hope they can get something like transition.finish to do such jobs.

How about moving the object to its final destination first, then using transition.from instead of transition.to? If you cancel the transition.from, the object should go straight to its final destination. I think. :slight_smile:

If you cancel a transition, regardless of to or from, wouldn’t it leave the display object in the spot where it’s at?

Do you want the transition to end when a user provides an input, or do you want it to end randomly, or on a timer? You could create a function that cancels the transition in question when each is fired. Something like this, for a touch input:

local trans1 local uiBox = display.newRect(0,0,32,32) local function finishTrans1() uiBox.x = display.contentWidth/4 uiBox.y = display.contentCenterY end local function playerInit(event) if event.phase == "ended" then transition.cancel(trans1) uiBox.x = display.contentWidth/4 uiBox.y = display.contentCenterY uiBox.xScale = 2 uiBox.yScale = 2 end end trans1 = transition.to(uiBox, {time=5000, alpha=.1, xScale=10, yScale=10, onComplete=finishTrans1}) Runtime:addEventListener("touch", playerInit)

Dear Panc,

Thanks. I did think of it, but doing it that way will be double job. Furthermore i have multiple transitions.

I was hoping for a function (maybe like transition.finish) to bring all the transition to an end instantly. 

I’m not sure what stimulus you are looking for, that would signal all transitions should be completed. You could modify the above to relate to a timer, or for something specific in the app. You’d have to code it yourself of course. 

Can you describe a bit more about your issue? Did you want to identify all transitions in the same “group”? What app behavior do you want to listen for, to signal that all transitions should be cancelled and completion be acted on?

Hi,

Could you use onCancel events and just use transition.cancel without parameters to cancel all ongoing transitions, something like:

[lua]

local function touchEvent(event)
    if event.phase==“began” then
        transition.cancel()
    end
end
local circle = display.newCircle(display.contentCenterX, display.contentCenterY,20)
local trans = transition.to(circle,{time=10000, x=0, y=0, xScale = 2,yScale = 2, onCancel = function(obj)
    obj.x = 0
    obj.y = 0
    obj.xScale = 2
    obj.yScale = 2
end})

local circle2 = display.newCircle(display.contentCenterX, display.contentCenterY,20)
local trans2 = transition.to(circle2,{time=10000, x=display.contentWidth, y=display.contentHeight, xScale = 4,yScale = 4, onCancel = function(obj)
    obj.x = display.contentWidth
    obj.y = display.contentHeight
    obj.xScale = 4
    obj.yScale = 4
end})

circle:addEventListener(“touch”, touchEvent)
circle2:addEventListener(“touch”, touchEvent)

[/lua]

local trans=transition.to( obj,{time=3000,x=320}

when you cancel the trans.
trans._target is the obj
trans._keysFinish is the params
so you can do
trans._target.x=trans._keysFinish.x
trans._target.y=trans._keysFinish.y
and so on for the other values
the onComplete function is
trans._onComplete()

So I suppose something like this could be used to complete all ongoing transitions to their final states when transitions are canceled:

[lua]

local function completeTransition(obj)
    if obj.trans then
        for key,value in pairs(obj.trans._keysFinish) do
            obj[key] = value
        end
        obj.trans = nil
    end
end

local function touchEvent(event)
    if event.phase==“began” then
        transition.cancel()
    end
end

local circle = display.newCircle(display.contentCenterX, display.contentCenterY,20)
circle.alpha = 0
local trans = transition.to(circle,{time=10000, x=0, y=0, xScale = 2,yScale = 2, alpha = 1, onCancel = function(obj) completeTransition(obj) end })
circle.trans = trans

local circle2 = display.newCircle(display.contentCenterX, display.contentCenterY,20)
local trans2 = transition.to(circle2,{time=10000, x=display.contentWidth, xScale = 4, yScale = 4, onCancel = function(obj) completeTransition(obj) end})
circle2.trans = trans2

circle:addEventListener(“touch”, touchEvent)
circle2:addEventListener(“touch”, touchEvent)

[/lua]

When you touch either of the circles all transitions are canceled and all transition properties are set to their final values. This approach requires the transition to be stored in the displayobject and could of course be handled a bit differently.

looks bout right

Thanks all for the insights and suggestions. It seems that the only way is to do a ‘double job’, where a separate function is called to ‘finalize the object in transition’. I’ll put it into the Corona request feature. Hope they can get something like transition.finish to do such jobs.

How about moving the object to its final destination first, then using transition.from instead of transition.to? If you cancel the transition.from, the object should go straight to its final destination. I think. :slight_smile: