Files
flame/examples/lib/stories/components/composability_example.dart
Lukas Klingsbo b5bdf4ec17 feat!: The HasTappableComponents mixin is no longer needed (#2450)
This PR is the second in a series of refactors that aim to simplify event handling in Flame. The approach is as follows:

    Added the MultiTapDispatcher component, which contains the logic that used to be within the HasTappableComponents mixin. This component is internal; it mounts to a FlameGame directly, and ensures that it is a singleton.
    Whenever any TapCallbacks component is added to a game, it automatically adds the MultiTapDispatcher component (unless there is already one), which in turn registers a tap gesture detector with GestureDetectorBuilder and rebuilds the game widget.

The end result is that now in order to make a component tappable you only need to add the TapCallbacks mixin to that component, everything else will be handled by the framework.

Consequently, the HasTappableComponents mixin is now empty and marked as deprecated.
2023-04-02 16:52:57 +00:00

82 lines
1.9 KiB
Dart

import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
class ComposabilityExample extends FlameGame {
static const String description = '''
In this example we showcase how you can add children to a component and how
they transform together with their parent, if the parent is a
`PositionComponent`. This example is not interactive.
''';
late ParentSquare parentSquare;
@override
bool debugMode = true;
@override
Future<void> onLoad() async {
parentSquare = ParentSquare(Vector2.all(200), Vector2.all(300))
..anchor = Anchor.center;
add(parentSquare);
}
@override
void update(double dt) {
super.update(dt);
parentSquare.angle += dt;
}
}
class ParentSquare extends RectangleComponent with HasGameRef {
static final defaultPaint = BasicPalette.white.paint()
..style = PaintingStyle.stroke;
ParentSquare(Vector2 position, Vector2 size)
: super(
position: position,
size: size,
paint: defaultPaint,
);
@override
Future<void> onLoad() async {
createChildren();
}
void createChildren() {
// All positions here are in relation to the parent's position
const childSize = 50.0;
final children = [
RectangleComponent.square(
position: Vector2(100, 100),
size: childSize,
angle: 2,
paint: defaultPaint,
),
RectangleComponent.square(
position: Vector2(160, 100),
size: childSize,
angle: 3,
paint: defaultPaint,
),
RectangleComponent.square(
position: Vector2(170, 150),
size: childSize,
angle: 4,
paint: defaultPaint,
),
RectangleComponent.square(
position: Vector2(70, 200),
size: childSize,
angle: 5,
paint: defaultPaint,
),
];
addAll(children);
}
}