Created
January 21, 2020 16:19
-
-
Save 01010111/9250842de3e79b03c25058964fc48e6d to your computer and use it in GitHub Desktop.
Simplest Flixel Player
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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