mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 10:38:17 +08:00
Refactor joystick (#876)
* Refactor joystick * Fix directional tests * Joystick example * Any PositionComponent can be used as knob and background * Add MarginButtonComponent * Fix JoystickExample * Update joystick docs * Fix joystick direction tests * Fix effect tests * Fix analyze issue * Update docs * Update docs * Move joystick to input export * Update packages/flame/lib/src/geometry/shape.dart Co-authored-by: Luan Nico <luanpotter27@gmail.com> * Add test and description for screenAngle * Update examples/lib/stories/controls/joystick_player.dart Co-authored-by: Erick <erickzanardoo@gmail.com> * Update doc/input.md Co-authored-by: Erick <erickzanardoo@gmail.com> * controls -> input in examples to align with export file * controls -> input * Add simple joystick example * Fix imports * velocity -> relativeDelta Co-authored-by: Luan Nico <luanpotter27@gmail.com> Co-authored-by: Erick <erickzanardoo@gmail.com>
This commit is contained in:
39
examples/lib/stories/input/keyboard.dart
Normal file
39
examples/lib/stories/input/keyboard.dart
Normal file
@ -0,0 +1,39 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flutter/services.dart' show RawKeyDownEvent, RawKeyEvent;
|
||||
|
||||
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
|
||||
void onKeyEvent(RawKeyEvent e) {
|
||||
final isKeyDown = e is RawKeyDownEvent;
|
||||
if (e.data.keyLabel == 'a') {
|
||||
velocity.x = isKeyDown ? -1 : 0;
|
||||
} else if (e.data.keyLabel == 'd') {
|
||||
velocity.x = isKeyDown ? 1 : 0;
|
||||
} else if (e.data.keyLabel == 'w') {
|
||||
velocity.y = isKeyDown ? -1 : 0;
|
||||
} else if (e.data.keyLabel == 's') {
|
||||
velocity.y = isKeyDown ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user