Files
raw-haxe-game/source/entities/Player.hx
Indrajith K L ad2056876e Initial Commit
* Adds Menu
* Adds Level Manager - LevelBase
* Adds Sample level
* Basic player movement
2022-04-28 03:27:44 +05:30

76 lines
1.4 KiB
Haxe

package entities;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.input.actions.FlxAction.FlxActionDigital;
import flixel.input.actions.FlxActionManager;
import flixel.util.FlxColor;
class Player extends FlxSprite
{
var actionsManager:FlxActionManager;
var _left:FlxActionDigital;
var _right:FlxActionDigital;
var _jump:FlxActionDigital;
var _jumpPower:Int = 200;
public function new(x:Int, y:Int)
{
super(x, y);
makeGraphic(16, 16, FlxColor.BLUE);
var runSpeed:Int = 80;
drag.x = runSpeed * 8;
acceleration.y = 420;
maxVelocity.set(runSpeed, _jumpPower);
_left = new FlxActionDigital().addKey(LEFT, PRESSED);
_right = new FlxActionDigital().addKey(RIGHT, PRESSED);
_jump = new FlxActionDigital().addKey(X, JUST_PRESSED);
if (actionsManager == null)
{
actionsManager = FlxG.inputs.add(new FlxActionManager());
}
actionsManager.addActions([_left, _right, _jump]);
}
override public function update(elapsed:Float):Void
{
acceleration.x = 0;
updateInputs();
super.update(elapsed);
}
function updateInputs()
{
if (_left.triggered)
moveLeft();
else if (_right.triggered)
moveRight();
if (_jump.triggered)
jump();
// if (_shoot.triggered)
// shoot();
}
function moveLeft()
{
acceleration.x -= drag.x;
}
function moveRight()
{
acceleration.x += drag.x;
}
function jump()
{
if (velocity.y == 0)
{
velocity.y = -_jumpPower;
}
}
}