mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 04:18:25 +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:
50
examples/lib/stories/input/tappables.dart
Normal file
50
examples/lib/stories/input/tappables.dart
Normal file
@ -0,0 +1,50 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class TappableSquare extends PositionComponent with Tappable {
|
||||
static final Paint _white = Paint()..color = const Color(0xFFFFFFFF);
|
||||
static final Paint _grey = Paint()..color = const Color(0xFFA5A5A5);
|
||||
|
||||
bool _beenPressed = false;
|
||||
|
||||
TappableSquare({Vector2? position})
|
||||
: super(
|
||||
position: position ?? Vector2.all(100),
|
||||
size: Vector2.all(100),
|
||||
);
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
super.render(canvas);
|
||||
canvas.drawRect(size.toRect(), _beenPressed ? _grey : _white);
|
||||
}
|
||||
|
||||
@override
|
||||
bool onTapUp(_) {
|
||||
_beenPressed = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
bool onTapDown(_) {
|
||||
_beenPressed = true;
|
||||
angle += 1.0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
bool onTapCancel() {
|
||||
_beenPressed = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class TappablesGame extends BaseGame with HasTappableComponents {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
add(TappableSquare()..anchor = Anchor.center);
|
||||
add(TappableSquare()..y = 350);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user