Files
flame/examples/lib/stories/effects/remove_effect_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

46 lines
1.2 KiB
Dart

import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
class RemoveEffectExample extends FlameGame {
static const description = '''
Click on any circle to apply a RemoveEffect, which will make the circle
disappear after a 0.5 second delay.
''';
@override
void onMount() {
super.onMount();
camera.viewport = FixedResolutionViewport(Vector2(400, 600));
final rng = Random();
for (var i = 0; i < 20; i++) {
add(_RandomCircle.random(rng));
}
}
}
class _RandomCircle extends CircleComponent with TapCallbacks {
_RandomCircle(double radius, {super.position, super.paint})
: super(radius: radius);
factory _RandomCircle.random(Random rng) {
final radius = rng.nextDouble() * 30 + 10;
final position = Vector2(
rng.nextDouble() * 320 + 40,
rng.nextDouble() * 520 + 40,
);
final paint = Paint()
..color = Colors.primaries[rng.nextInt(Colors.primaries.length)];
return _RandomCircle(radius, position: position, paint: paint);
}
@override
void onTapDown(TapDownEvent info) {
add(RemoveEffect(delay: 0.5));
}
}