mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +08:00
docs: Improve keyboard input examples (#1907)
This PR updates the live keyboard examples to correctly handle multiple key inputs. This was discovered in #847.
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import 'package:examples/commons/ember.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
@ -9,26 +8,28 @@ 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 A S D W to steer Ember.
|
||||
Usage: Use WASD to steer Ember.
|
||||
''';
|
||||
|
||||
static final Paint white = BasicPalette.white.paint();
|
||||
static const int speed = 200;
|
||||
// Speed at which amber moves.
|
||||
static const double _speed = 200;
|
||||
|
||||
late final Ember ember;
|
||||
final Vector2 velocity = Vector2(0, 0);
|
||||
// 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);
|
||||
_ember = Ember(position: size / 2, size: Vector2.all(100));
|
||||
add(_ember);
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
super.update(dt);
|
||||
final displacement = velocity * (speed * dt);
|
||||
ember.position.add(displacement);
|
||||
final displacement = _direction.normalized() * _speed * dt;
|
||||
_ember.position.add(displacement);
|
||||
}
|
||||
|
||||
@override
|
||||
@ -38,14 +39,18 @@ class KeyboardExample extends FlameGame with KeyboardEvents {
|
||||
) {
|
||||
final isKeyDown = event is RawKeyDownEvent;
|
||||
|
||||
if (event.logicalKey == LogicalKeyboardKey.keyA) {
|
||||
velocity.x = isKeyDown ? -1 : 0;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.keyD) {
|
||||
velocity.x = isKeyDown ? 1 : 0;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.keyW) {
|
||||
velocity.y = isKeyDown ? -1 : 0;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.keyS) {
|
||||
velocity.y = isKeyDown ? 1 : 0;
|
||||
// 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);
|
||||
|
||||
Reference in New Issue
Block a user