Set velocity
on a FlxObject
to make it move at a constant rate, in pixels per second.
// object will move from left to right at 50 pixels per second
object.velocity.x = 50;
Set velocity
on a FlxObject
to make it move at a constant rate, in pixels per second.
// object will move from left to right at 50 pixels per second
object.velocity.x = 50;
package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
class PlayState extends FlxState
{
var sprite:FlxSprite;
override function create()
{
super.create();
sprite = new FlxSprite("assets/sprite.png");
sprite.y = FlxG.height / 2 - sprite.height / 2;
add(sprite);
sprite.velocity.x = 100;
}
override function update(elapsed:Float)
{
if (sprite.velocity.x > 0 && sprite.x >= FlxG.width - sprite.width)
{
sprite.x = FlxG.width - sprite.width;
sprite.velocity.x *= -1;
}
else if (sprite.velocity.x < 0 && sprite.x <= 0)
{
sprite.x = 0;
sprite.velocity.x *= -1;
}
super.update(elapsed);
}
}