Reputation: 51
I'm currently animating a trash can when something gets dragged and dropped onto it with this code:
local trashUp
local trashDown
trashUp = function()
transition.to(
trash, {time=100, xScale=1.2, yScale=1.2, onComplete=trashDown })
end
trashDown = function()
transition.to(
trash, {time=100, xScale=1, yScale=1})
end
and then calling trashUp() when I want to start the animation.
The code works fine, but I can't help feel it could be coded better. Two functions to animate an object!
Is there any way I can do this more efficiently?
Upvotes: 3
Views: 2219
Reputation: 595
transition.to( trash, {time=t, delta=true, xScale=1.5, transition=easing.continousLoop} )
Also very useful for purposes like that:
easing.sin = function( f, a )
return function(t, tmax, start, d)
return start + delta + a*math.sin( (t/tmax) *f * math.pi*2 )
end
end
easing.sinDampened = function( f, a, damp )
return function(t, tmax, start, d)
return start + delta + a*math.sin( damp^(t/tmax) *f * math.pi*2 )
end
end
...etc
Upvotes: 0
Reputation: 415
This question is pretty old, but since I was trying to do the same thing, I figured I should share what I came up with. This sequentially executes transitions that are passed in as variable args. If onComplete is supplied, it is called as its transition completes.
local function transitionSequence(target, step, ...)
local remaining_steps = {...}
if #remaining_steps > 0 then
local originalOnComplete = step.onComplete
step.onComplete = function(target)
if originalOnComplete then
originalOnComplete(target)
end
transitionSequence(target, unpack(remaining_steps))
end
transition.to(target, step)
else
transition.to(target, step)
end
end
Example:
transitionSequence(myImage,
{xScale=0.5, onComplete=function(t) print("squeeze") end},
{xScale=1, onComplete=function(t) print("relax") end},
{yScale=2, onComplete=function(t) print("stretch") end},
{yScale=1, onComplete=function(t) print("relax again") end})
Upvotes: 0
Reputation: 3039
You can do it by coding the onComplete
function inline with the first transition call:
animateTrash = function()
transition.to(
trash,
{ time=100, xScale=1.2, yScale=1.2, onComplete=
function()
transition.to(
trash,
{time=100, xScale=1, yScale=1})
end
})
end
This won't be any more efficient, but it does keep everything to do with animating the trashcan in one place. I think this approach could get quickly out of hand, though, especially if you were doing anything other than the transitions when animating the trashcan, e.g., updating your program's state, or creating/destroying other objects, etc.
Echoing jhocking's answer, this approach doesn't support canceling either.
Upvotes: 1
Reputation: 5577
Well you could do it in a single function by setting the second transition with delay; refer to this code example: http://developer.anscamobile.com/reference/index/transitionto
Depending on your situation however that's not necessarily less complicated, because now you have to keep track of two transitions simultaneously instead of just one transition at a time. In the code you posted you aren't keeping track of the transitions, but you probably should be in case you need to cancel them before the transition is complete (eg. the player switches scenes in the middle of the transition).
Upvotes: 1