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.dartfile and change the class definition to include theHasKeyboardHandlerComponentsmixin: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.dartfile where we will listen for keyboard events and change the class definition to add theKeyboardHandlermixin:class George extends Character...