mirror of
https://github.com/flame-engine/flame.git
synced 2025-10-31 00:48:47 +08:00
* Add keyboard with focus node implementation * a * format and make it stabel compatible * Add mixin to game * fixes * add tests * format * docs * more docs * Update doc/keyboard-input.md Co-authored-by: Erick <erickzanardoo@gmail.com> * rename test * Apply suggestions from code review Co-authored-by: Luan Nico <luanpotter27@gmail.com> * fix test * Update tutorials/2_sprite_animations_gestures/README.md Co-authored-by: Luan Nico <luanpotter27@gmail.com> * docs * Apply suggestions from code review Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net> * yo Co-authored-by: Erick <erickzanardoo@gmail.com> Co-authored-by: Luan Nico <luanpotter27@gmail.com> Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net>
48 lines
1.3 KiB
Dart
48 lines
1.3 KiB
Dart
import 'dart:ui';
|
|
|
|
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';
|
|
|
|
class KeyboardGame extends Game with KeyboardEvents {
|
|
static final Paint white = BasicPalette.white.paint();
|
|
static const int speed = 200;
|
|
|
|
Rect rect = const Rect.fromLTWH(0, 100, 100, 100);
|
|
final Vector2 velocity = Vector2(0, 0);
|
|
|
|
@override
|
|
void update(double dt) {
|
|
final displacement = velocity * (speed * dt);
|
|
rect = rect.translate(displacement.x, displacement.y);
|
|
}
|
|
|
|
@override
|
|
void render(Canvas canvas) {
|
|
canvas.drawRect(rect, white);
|
|
}
|
|
|
|
@override
|
|
KeyEventResult onKeyEvent(
|
|
RawKeyEvent event,
|
|
Set<LogicalKeyboardKey> keysPressed,
|
|
) {
|
|
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;
|
|
}
|
|
|
|
return super.onKeyEvent(event, keysPressed);
|
|
}
|
|
}
|