Skip to content

Instantly share code, notes, and snippets.

@DleanJeans
Created November 2, 2016 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DleanJeans/97e8d94f44065ebf21d56f36e0842274 to your computer and use it in GitHub Desktop.
Save DleanJeans/97e8d94f44065ebf21d56f36e0842274 to your computer and use it in GitHub Desktop.
package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.math.FlxPoint;
class PlayState extends FlxState {
public static inline var SPEED = 1000;
public var sprite:FlxSprite;
public var switchFramerate:Bool = true;
override public function create() {
sprite = new FlxSprite();
sprite.makeGraphic(50, 50);
sprite.screenCenter();
sprite.maxVelocity.set(100, 100);
add(sprite);
FlxG.drawFramerate = 10;
FlxG.watch.add(this, "switchFramerate");
FlxG.watch.add(FlxG, "fixedTimestep");
FlxG.watch.add(FlxG, "elapsed");
FlxG.watch.add(FlxG, "drawFramerate");
FlxG.watch.add(sprite, "x", "x");
FlxG.watch.add(sprite, "y", "y");
}
override public function update(elapsed:Float) {
super.update(elapsed);
if (FlxG.keys.justPressed.SPACE)
FlxG.fixedTimestep = !FlxG.fixedTimestep;
if (FlxG.keys.justPressed.ENTER) {
switchFramerate = !switchFramerate;
if (!switchFramerate)
FlxG.drawFramerate = 60;
}
if (FlxG.keys.pressed.A)
sprite.velocity.x = -SPEED;
if (FlxG.keys.pressed.D)
sprite.velocity.x = SPEED;
if (FlxG.keys.pressed.W)
sprite.velocity.y = -SPEED;
if (FlxG.keys.pressed.S)
sprite.velocity.y = SPEED;
if (!FlxG.keys.anyPressed(["A", "D", "W", "S"]))
sprite.velocity.set();
if (switchFramerate && sprite.velocity.equals(FlxPoint.weak())) {
FlxG.drawFramerate = 10;
}
else FlxG.drawFramerate = 60;
FlxG.watch.addQuick("updateFramerate", 1 / FlxG.elapsed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment