Skip to content

Instantly share code, notes, and snippets.

@Beeblerox
Created January 5, 2014 10:40
Show Gist options
  • Save Beeblerox/8266663 to your computer and use it in GitHub Desktop.
Save Beeblerox/8266663 to your computer and use it in GitHub Desktop.
Helper state for getting codes for button on gamepad
package ;
import flixel.FlxG;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.util.FlxColor;
#if cpp
import openfl.events.JoystickEvent;
#end
/**
* ...
* @author Zaphod
*/
class MenuState extends FlxState
{
var message:FlxText;
var messageString:String;
override public function create():Void
{
FlxG.cameras.bgColor = FlxColor.GRAY;
#if cpp
FlxG.stage.addEventListener(JoystickEvent.BUTTON_DOWN, onJoystickButtonDown);
FlxG.stage.addEventListener(JoystickEvent.BUTTON_UP, onJoystickButtonUp);
#end
messageString = "";
message = new FlxText(10, 10, FlxG.width - 10, messageString, 16);
message.setFormat(null, 16, 0x000000, "left");
add(message);
super.create();
}
#if cpp
private function onJoystickButtonUp(e:JoystickEvent):Void
{
messageString += "\nButtonUp: " + e.id;
}
private function onJoystickButtonDown(e:JoystickEvent):Void
{
messageString += "\nButtonDown: " + e.id;
}
#end
override public function update():Void
{
var lines:Array<String> = messageString.split("\n");
var maxLines:Int = 10;
if (lines.length > maxLines)
{
messageString = "";
for (i in (lines.length - maxLines)...lines.length)
{
if (i != (lines.length - maxLines))
{
messageString += "\n";
}
messageString += lines[i];
}
}
message.text = messageString;
super.update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment