Files
flame/examples/lib/stories/input/keyboard_example.dart
DevKage 5bdb2a1ec8 docs: Improve keyboard input examples (#1907)
This PR updates the live keyboard examples to correctly handle multiple key inputs. This was discovered in #847.
2022-09-15 08:13:05 +02:00

59 lines
1.7 KiB
Dart

import 'package:examples/commons/ember.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
class KeyboardExample extends FlameGame with KeyboardEvents {
static const String description = '''
Example showcasing how to act on keyboard events.
It also briefly showcases how to create a game without the FlameGame.
Usage: Use WASD to steer Ember.
''';
// Speed at which amber moves.
static const double _speed = 200;
// Direction in which amber is moving.
final Vector2 _direction = Vector2.zero();
late final Ember _ember;
@override
Future<void> onLoad() async {
_ember = Ember(position: size / 2, size: Vector2.all(100));
add(_ember);
}
@override
void update(double dt) {
super.update(dt);
final displacement = _direction.normalized() * _speed * dt;
_ember.position.add(displacement);
}
@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final isKeyDown = event is RawKeyDownEvent;
// Avoiding repeat event as we are interested only in
// key up and key down event.
if (!event.repeat) {
if (event.logicalKey == LogicalKeyboardKey.keyA) {
_direction.x += isKeyDown ? -1 : 1;
} else if (event.logicalKey == LogicalKeyboardKey.keyD) {
_direction.x += isKeyDown ? 1 : -1;
} else if (event.logicalKey == LogicalKeyboardKey.keyW) {
_direction.y += isKeyDown ? -1 : 1;
} else if (event.logicalKey == LogicalKeyboardKey.keyS) {
_direction.y += isKeyDown ? 1 : -1;
}
}
return super.onKeyEvent(event, keysPressed);
}
}