How can i tween an image between two positions? Yet again, its for the game im making,
Thanks!
Tweening
Re: Tweening
If I remember correctly, 'tweening' in Flash is a bit of a general term that includes modulating/alpha blending the sprite not only transforming it.Anxiety wrote:How can i tween an image between two positions? Yet again, its for the game im making
If you're only trying to change position of a sprite you could trying something like:
Code: Select all
elapsed = elapsed + delta
local t = math.min ( elapsed / totalTime, 1 )
x = (fpx - ipx) * t
y = (fpy - ipy) * t
if t == 1 then
-- target reached
end
I personally find it easeier to use a velocity instead of storing the 'total' and 'elapsed' time:
Code: Select all
local dx = fpx - x
local dy = fpy - y
local dist = math.sqrt ( dx * dx + dy * dy )
local step = velocity * delta
if dist <= step then
x = fpx
y = fpy
-- target reached
else
x = x + dx / dist * step
y = y + dy / dist * step
end
Last edited by ivan on Wed Apr 13, 2011 6:02 am, edited 1 time in total.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Tweening
The most raw way is to use a timer and some target variables. For example:
You could then use current as the position. However, this approach quickly gets messy. I personally create a class to handle it all for me, but EmmanuelOga created a tweening library which you might want to use.
EDIT: Ninja'd. (At least with showing a raw method anyway)
Code: Select all
time = 0 -- the current time
target = 1 -- the target time
start = 0 -- the starting value
targetVal = 300 -- the target value
function love.update(dt)
time = time + dt
current = start + (targetVal - start) * (time / target)
end
EDIT: Ninja'd. (At least with showing a raw method anyway)
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Tweening
vrld's HUMP library will help with interpolations (tweens) too. http://vrld.github.com/hump/#timer-Interpolator
- EmmanuelOga
- Citizen
- Posts: 56
- Joined: Thu Apr 22, 2010 9:42 pm
- Location: Buenos Aires, Argentina
- Contact:
Re: Tweening
Here are a couple of examples you can try which use the "tweener" library.
https://github.com/EmmanuelOga/tweener/ ... mples/love
To run it, get git. Download it here:
https://github.com/EmmanuelOga/tweener/zipball/master
then:
cd tweener;
love examples/love/follow
love examples/love/circle
love examples/love/simple
love examples/love/size_and_pos
https://github.com/EmmanuelOga/tweener/ ... mples/love
To run it, get git. Download it here:
https://github.com/EmmanuelOga/tweener/zipball/master
then:
cd tweener;
love examples/love/follow
love examples/love/circle
love examples/love/simple
love examples/love/size_and_pos
--------------------------------------------------------------------------------------------------------
http://EmmanuelOga.com
http://EmmanuelOga.com
Who is online
Users browsing this forum: No registered users and 13 guests