Other Inputs and Helpers¶
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 out the following example to get a better understanding:
class MyGame extends FlameGame {
@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 Player extends SpriteComponent with HasGameRef {
Player(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.relativeDelta * maxSpeed * dt);
angle = joystick.delta.screenAngle();
}
}
}
In this example, we created the classes MyGame
and Player
.
MyGame
creates a joystick which is passed to the Player
when the latter 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.
Following fields 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.relativeDelta
: 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
.
For the complete code on implementing the joystick, check out the Joystick Example. You can also view the JoystickComponent in action to see a live example of the joystick input function integrated into a game.
For an additional challenge, explore the Advanced Joystick Example. See what else the advanced features can do in the live demo.
Gamepad¶
Flame has a dedicated plugin to support external game controllers (gamepads). Find more information in the Gamepads repository.
IgnoreEvents mixin¶
If you don’t want a component subtree to receive events, you can use the IgnoreEvents
mixin.
Once you have added this mixin you can turn off events to reach a component and its descendants by
setting ignoreEvents = true
(default when the mixin is added), and then set it to false
when you
want to receive events again.
This can be done for optimization purposes, since all events currently go through the whole component tree.