Other Inputs¶
This includes documentation for input methods besides keyboard and mouse.
For other input documents, see also:
Gesture Input: for mouse and touch pointer gestures
Keyboard Input: for keystrokes
Joystick¶
Flame provides a component capable of creating a virtual joystick for taking input for your game.
To use this feature, you need to create a JoystickComponent
, configure it the way you want, and
add it to your game.
Check this example to get a better understanding:
class MyGame extends FlameGame with HasDraggables {
MyGame() {
joystick.addObserver(player);
add(player);
add(joystick);
}
@override
Future<void> onLoad() async {
super.onLoad();
final image = await images.load('joystick.png');
final sheet = SpriteSheet.fromColumnsAndRows(
image: image,
columns: 6,
rows: 1,
);
final joystick = JoystickComponent(
knob: SpriteComponent(
sprite: sheet.getSpriteById(1),
size: Vector2.all(100),
),
background: SpriteComponent(
sprite: sheet.getSpriteById(0),
size: Vector2.all(150),
),
margin: const EdgeInsets.only(left: 40, bottom: 40),
);
final player = Player(joystick);
add(player);
add(joystick);
}
}
class JoystickPlayer extends SpriteComponent with HasGameRef {
JoystickPlayer(this.joystick)
: super(
anchor: Anchor.center,
size: Vector2.all(100.0),
);
/// Pixels/s
double maxSpeed = 300.0;
final JoystickComponent joystick;
@override
Future<void> onLoad() async {
sprite = await gameRef.loadSprite('layers/player.png');
position = gameRef.size / 2;
}
@override
void update(double dt) {
if (joystick.direction != JoystickDirection.idle) {
position.add(joystick.velocity * maxSpeed * dt);
angle = joystick.delta.screenAngle();
}
}
}
So in this example, we create the classes MyGame
and Player
. MyGame
creates a joystick which is
passed to the Player
when it is created. In the Player
class we act upon the current state of
the joystick.
The joystick has a few fields that change depending on what state it is in. These are the fields that should be used to know the state of the joystick:
intensity
: The percentage [0.0, 1.0] that the knob is dragged from the epicenter to the edge of the joystick (orknobRadius
if that is set).delta
: The absolute amount (defined as aVector2
) that the knob is dragged from its epicenter.velocity
: The percentage, presented as aVector2
, and direction that the knob is currently pulled from its base position to a edge of the joystick.
If you want to create buttons to go with your joystick, check out
HudButtonComponent
.
A full examples of how to use it can be found here. And it can be seen running here.
There is also a more advanced example here which is running here.
Gamepad¶
Flame has a separate plugin to support external game controllers (gamepads), check here for more information.