Use maxVelocity
to limit the velocity
of a FlxObject
. Very useful when using acceleration
to ‘cap’ the object’s speed.
// object's x velocity will stay between -100 and +100
object.maxVelocity.x = 100;
Use maxVelocity
to limit the velocity
of a FlxObject
. Very useful when using acceleration
to ‘cap’ the object’s speed.
// object's x velocity will stay between -100 and +100
object.maxVelocity.x = 100;
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;
sprite.acceleration.x = 320;
add(sprite);
sprite.maxVelocity.x = 160;
}
override function update(elapsed:Float)
{
super.update(elapsed);
if (sprite.acceleration.x > 0 && sprite.x >= FlxG.width - sprite.width)
{
sprite.x = FlxG.width - sprite.width;
sprite.velocity.x = 0;
sprite.acceleration.x *= -1;
}
else if (sprite.acceleration.x < 0 && sprite.x <= 0)
{
sprite.x = 0;
sprite.velocity.x = 0;
sprite.acceleration.x *= -1;
}
}
}