mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +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:
43
examples/lib/stories/input/scroll.dart
Normal file
43
examples/lib/stories/input/scroll.dart
Normal file
@ -0,0 +1,43 @@
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ScrollGame extends BaseGame with ScrollDetector {
|
||||
static const speed = 2000.0;
|
||||
final _size = Vector2.all(50);
|
||||
final _paint = BasicPalette.white.paint();
|
||||
|
||||
Vector2 position = Vector2.all(100);
|
||||
Vector2? target;
|
||||
|
||||
@override
|
||||
void onScroll(PointerScrollInfo info) {
|
||||
target = position + info.scrollDelta.game * 5;
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
super.render(canvas);
|
||||
canvas.drawRect(position.toPositionedRect(_size), _paint);
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
super.update(dt);
|
||||
final target = this.target;
|
||||
final ds = speed * dt;
|
||||
if (target != null) {
|
||||
if (position != target) {
|
||||
final diff = target - position;
|
||||
if (diff.length < ds) {
|
||||
position.setFrom(target);
|
||||
} else {
|
||||
diff.scaleTo(ds);
|
||||
position.setFrom(position + diff);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user