flame_signals

Last updated:

0 purchases

flame_signals Image
flame_signals Images
Add to Cart

Description:

flame signals

#Flame Signals
flame_signals offers a simple, type-safe mechanism to provide signals with or without data payloads
between components in a FlameGame.
Follow this link for a simple example. This is the example in the github repo.
How To Use #
Add To Game #
To use flame_signals, start by adding the HasSignals mixin to your game class:
class SpaceGame extends FlameGame with HasSignals {
//...
}
copied to clipboard
The mixin defines a Stream of type FlameSignal to adding and broadcasting the signals.
Define a signal class #
Extend the base class FlameSignal to define one or more signal objects.
class MeteorDestroyedSignal extends FlameSignal {
final Vector2 position;
final double laserAngle;

const MeteorDestroyedSignal({required this.position, required this.laserAngle});
}

class DecreaseMeteorCountSignal extends FlameSignal {}
copied to clipboard
Send a signal #
Any component in the tree will be able to send a FlameSignal. Both the SendSignals and
FlameSignalListenable mixins provide an addSignal method to send out a signal.
class Meteor extends Component with SendSignals {
//...
//...
void onHitByLaser() {
addSignal(MeteorDestroyedSignal(
position: position,
laserAngle: laser.angle,
));
}
}
copied to clipboard
React to a signal received. #
Components that need to react to game signal use the FlameSignalListenable mixin.
A signal handler must be registered in the components onMount life cycle method.
This creates a subscription to the signal stream. When the component is removed from
the component tree, the onRemove life cycle method cancels the subscription.
class MeteorManager extends Component with FlameSignalListenable {
@override
void onMount() {
super.onMount();
onSignal<MeteorDestroyedSignal>((signal) {
_generateNewMeteor();
_updateMeteorCount();
});
}
}
copied to clipboard
A component using the FlameSignalListenable mixin can also add signals.
class MeteorManager extends Component with FlameSignalListenable {
@override
void onMount() {
super.onMount();
onSignal<MeteorDestroyedSignal>((signal) {
_generateNewMeteor();
_updateMeteorCount();
});
}
//...
//...
void _updateMeteorCount() {
addSignal(DecreaseMeteorCountSignal());
}
}
copied to clipboard
For more information #
GitHub Repo
Example App (found in repo)

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.