mirror of
https://github.com/flame-engine/flame.git
synced 2025-10-30 08:27:36 +08:00
* Animations, CameraAndViewport, CollisionDetection and Components unified * Added descriptions to effects * Rename input games * Unify input stories * Add info to parallax section * Added descriptions to the rendering examples * Add descriptions to the sprites directory * Fix utils and rendering section * Add descriptions to the widgets section * Delete directory that rebase brought back * Unify game names * Added some styleguide docs for examples * Fix analyze issues * All files should have _example as suffix * Made the FollowComponentExample a bit easier to understand * Change priority of ember
58 lines
1.6 KiB
Dart
58 lines
1.6 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';
|
|
|
|
import '../../commons/ember.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 A S D F to steer Ember.
|
|
''';
|
|
|
|
static final Paint white = BasicPalette.white.paint();
|
|
static const int speed = 200;
|
|
|
|
late final Ember ember;
|
|
final Vector2 velocity = Vector2(0, 0);
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
await super.onLoad();
|
|
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);
|
|
}
|
|
|
|
@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);
|
|
}
|
|
}
|