Skip to content

Instantly share code, notes, and snippets.

@01010111
Created January 21, 2020 16:19
Show Gist options
  • Save 01010111/9250842de3e79b03c25058964fc48e6d to your computer and use it in GitHub Desktop.
Save 01010111/9250842de3e79b03c25058964fc48e6d to your computer and use it in GitHub Desktop.
Simplest Flixel Player
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
class Player extends FlxSprite {
var WALK_SPEED = 200;
var JUMP_POWER = 300;
var FRAME_WIDTH = 16;
var FRAME_HEIGHT = 16;
var GRAVITY = 980;
public function new(x:Float, y:Float) {
super(x, y);
loadGraphic('assets/images/my_player_graphic.png', true, FRAME_WIDTH, FRAME_HEIGHT);
animation.add('idle', [0]);
animation.add('walk', [1, 2, 3, 4], 24);
animation.add('jump', [5]);
setFacingFlip(FlxObject.LEFT, true, false);
setFacingFlip(FlxObject.RIGHT, false, false);
acceleration.y = GRAVITY;
}
override function update(elapsed:Float) {
super.update(elapsed);
controls();
animations();
}
function controls() {
velocity.x = 0;
if (FlxG.keys.pressed.LEFT) velocity.x -= WALK_SPEED;
if (FlxG.keys.pressed.RIGHT) velocity.x += WALK_SPEED;
if (FlxG.keys.justPressed.SPACE && isTouching(FlxObject.FLOOR)) velocity.y -= JUMP_POWER;
}
function animations() {
if (!isTouching(FlxObject.FLOOR)) animation.play('jump');
else if (velocity.x != 0) animation.play('walk');
else animation.play('idle');
if (velocity.x > 0) facing = FlxObject.RIGHT;
else if (velocity.x < 0) facing = FlxObject.LEFT;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment