Using these tweens is not difficult. When you want your sprite to move from point A to point B, first create a Tween object. The constructor takes 4 arguments.
- The initial position as an array, AKA "Point A." This should be the current position of your sprite.
- The desired final position as an array, AKA "Point B."
- The easing module to use. Play with the demo and choose an easing module (the name of which is displayed at the top of the screen).
- The duration of the tween in seconds. In other words, how long do you want it to take for the sprite to get from point A to point B.
tween = Tween.new( [ sprite.x, sprite.y ], [ sprite.x + 100, sprite.y + 100 ], Tween::Quart::InOut, 1 )
On every frame, pass the number of seconds since the last frame. This is commonly referred to as the "delta." Calculating deltas is discussed here. Then, update your sprite's position with the tween's X and Y attributes.
tween.update(delta) sprite.x, sprite.y = tween.x, tween.y
When the tween is done and the sprite is at point B, the tween's done method will return true. At any point before that, it will return false.
if tween.done # We're at point B, do something else now! end
This article is part of a series. Read more articles about Rapid Game Prototyping in Ruby