Input, Event and Listener - Rombusevil/flixel-gdx GitHub Wiki
In this guide you’ll learn how to use the different inputs in flixel. Most of them are accessible via FlxG
(the Global helper class).
The Mouse
input can be reach via FlxG.mouse
. Then you get access to all mouse functions like just pressed, pressed and just released. The Mouse
also acts as touch input for touch screen devices. For touch screen devices, multi touch is also supported.
The functions of mouse return a Boolean whether the event has occurred.
FlxG.mouse.justPressed() |
The mouse was just pressed. |
FlxG.mouse.pressed() |
The mouse is pressed. |
FlxG.mouse.justReleased() |
The mouse is just released. |
FlxG.mouse.getWorldPosition() |
Get the coordinates of the mouse in world position. |
FlxG.mouse.getScreenPosition() |
Get the coordinates of the mouse in screen position. |
FlxG.mouse.screenX / screenY |
Get the coordinates of the mouse in screen position. |
Examples:
Doing multitouch goes via the same methods as described in the table above, but than with an argument passed to determine which pointer/finger has been used. The snippet below loops through 10 indices (our ten fingers) to check which index is pressed.
for(int i = 0; i < 10; i++)
{
if(FlxG.mouse.pressed(i))
{
FlxG.log("input: " + i);
}
}
Examples:
The Keyboard
input can be reached via FlxG.keys
. The Keyboard
has collection of keys and can be check checked via variety ways.
FlxG.keys.pressed("SPACE") |
Check if the button is pressed. |
FlxG.keys.SPACE |
Quick check equal to FlxG.keys.pressed("SPACE") . |
FlxG.keys.justPressed("SPACE") |
Check if the button was just pressed. |
FlxG.keys.justReleased("SPACE") |
Check if the button was just released. |
Examples:
If you want reconfigurable button support in your game, you can store the key name in a String and use it in the Keyboard
's methods.
String jump = "SPACE";
FlxG.keys.justPressed(jump);
GamepadManager
, is the plugin class. You’ll need to register this class via FlxG.addPlugin()
. This class manages the controllers and passes the input events to the correct Gamepad
. It also does auto mapping.
GamepadMapping
, contains a bunch of indices for each gamepad button. If you want to map a certain gamepad, you need to change the indices.
Gamepad
, it’s like the Keyboard
class. It tracks what keys are pressed, tracks analog and accelerometer data.
The gamepad uses the default mapping that is shipped with libGDX. Hooking a gamepad like Xbox 360 won’t work. You’ll need to create a mapping first. Click here for the mapping for Xbox 360. There are different models of controllers with different IDs. The ID is crucial for doing auto mapping. Via GamepadManager
you can add the mapping.
Adding a gamepad is very simple, just add new Gamepad
to the manager. When you do this, the manager will automatically set the correct mapping to the gamepad. It reads the ID of the controller. So be sure the ID is correct written.
// Xbox 360 Controller (ID varies). GamepadManager.addMapping(new Xbox360Controller());
// Create gamepads. You can add as many as you like. GamepadManager.addGamepad(gamepad = new Gamepad());
// Xbox 360 Controller (ID varies).
GamepadManager.addMapping(new Xbox360Controller());
// Create gamepads. You can add as many as you like.
GamepadManager.addGamepad(gamepad = new Gamepad());
Getting the state of the buttons, you can get it like this: gamepad.justPressed("Button_X")
. Look at this example.
If you want to add your own mapping, you’ll need to read the buttonCode
. If you develop from source you can add a log in GamepadManager.buttonDown
and axisMoved
.
Examples:
Flixel comes with two on screen controllers: FlxVirtualPad
and FlxAnalog
. They are easy in use by simply adding them to the state.
FlxVirtualPad
’s constructor requires two arguments. You can customize the layout by passing FlxVirtualPad.FULL
for 4 direction buttons and FlxVirtualPad.A_B_C
for three action buttons. You can get the status of the button by FlxVirtualPad.buttonLeft.status
.
FlxVirtualPad pad = new FlxVirtualPad(FlxVirtualPad.FULL, FlxVirtualPad.A);
if(pad.buttonLeft.status == FlxButton.PRESSED)
{
// Do something
}
You can customize the graphics by overriding FlxVirtualPad.createButton()
and createCenter()
.
Examples:
The constructor of FlxAnalog
has 4 arguments (two optional).
- The first two are the position.
- The third is the radius where the thumb can move.
- The fourth is the radius where the pointer (mouse/finger) can move while dragging the thumb. If the pointer is outside the radius, the thumb will be released.
You can get the x
,y
data via FlxAnalog.acceleration
.
Examples:
The gesture detector is part of the input processor. To use gesture you only need to create a FlxGesture
object and call start()
.
FlxGesture gesture = new FlxGesture();
gesture.start(new IFlxGesture()
{
@Override
public void callback(int Gesture, GestureData data)
{
switch(Gesture)
{
case FlxGesture.ZOOM:
text.setText("ZOOM");
break;
}
}
}
In start()
you need to pass a callback. The callback returns the type and data that belongs to the gesture that got performed. The type can be accessed via FlxGesture
. The GestureData
holds information like the position, velocity, distance between two fingers, etc.
Examples:
The sensor is part of the input processor and can be accessed via FlxG.sensor
. The Sensor
class got various attributes.
x |
The value of the accelerometer on its x-axis. ranges between [-10,10].
|
y |
The value of the accelerometer on its x-axis. ranges between [-10,10].
|
z |
The value of the accelerometer on its y-axis. ranges between [-10,10].
|
azimuth |
The azimuth is the angle of the device's orientation around the z-axis. The positive z-axis points towards the earths center. |
pitch |
The pitch is the angle of the device's orientation around the x-axis. The positive x-axis roughly points to the west and is orthogonal to the z- and y-axis. |
roll |
The roll is the angle of the device's orientation around the y-axis. The positive y-axis points to the magnetic north pole of the earth. |
rotation |
The rotation of the device with respect to its native orientation. |
Examples: