Navigating with key events
Our game already allows the player character to be controlled with either the joystick or touch events, but for websites, a more common method would be to use the keyboard to control the character.
In this section, we will add keyboard control as another option, so let's get started.
To listen for keyboard events in the game, we first have to tell our game that some of our components will listen for keyboard events:
- Open the
main.dart
file and change the class definition to include theHasKeyboardHandlerComponents
mixin:class GoldRush extends FlameGame with HasCollidables, HasDraggables, HasTappables, HasKeyboardHandlerComponents {
- Add the following input import at the top of the same file:
import 'package:flame/input.dart';
- Open the
george.dart
file where we will listen for keyboard events and change the class definition to add theKeyboardHandler
mixin:class George extends Character...