Skip to content

Controller

Will Blanton edited this page Sep 12, 2019 · 1 revision

A Controller class using standard controller buttons (XBox360 Controller)

You can extend this class to create your own controller classes - I like to use it as a mediator between an AI script and an enemy/npc!

You should override set() to set the controls (instead of update()):

override function set(elapsed:Float)
{
	set_button(DPAD_LEFT, parent.facing == LEFT);
	set_button(DPAD_LEFT, parent.facing == RIGHT);
}

Add the controller to your game state by using Controller.add() instead of FlxState.add():

override public function create()
{
	var controller:Controller = new Controller();
	controller.add() // Correct!
	// add(controller); Incorrect!
}

Check the controller's state with pressed, just_pressed, and just_released:

if (controller.pressed(DPAD_LEFT)) velocity.x = -100;
if (controller.pressed(DPAD_RIGHT)) velocity.x = 100;
if (controller.just_pressed(FACE_A)) jump();
Clone this wiki locally