mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
Add documentation for SequenceEffect
This commit is contained in:
@ -66,6 +66,24 @@ square.addEffect(RotateEffect(
|
||||
));
|
||||
```
|
||||
|
||||
## SequenceEffect
|
||||
|
||||
This effect is a combination of other effects. You provide it with a list of your predefined effects, that are not added to a component.
|
||||
|
||||
Note that no effects added to the sequence can have their `isInfinite` property set to `true`, because then naturally the sequence will get stuck once it gets to that effect.
|
||||
|
||||
You can make the sequence go in a loop by setting both `isInfinite: true` and `isAlternating: true`.
|
||||
|
||||
Usage example:
|
||||
```dart
|
||||
final sequence = SequenceEffect(
|
||||
effects: [move1, scale, move2, rotate],
|
||||
isInfinite: true,
|
||||
isAlternating: true);
|
||||
myComponent.addEffect(sequence);
|
||||
```
|
||||
An example of how to use the SequenceEffect can be found [here](/doc/examples/effects/sequence_effect).
|
||||
|
||||
# Examples
|
||||
|
||||
Full examples can be found [here](/doc/examples/effects/simple) and [here](/doc/examples/effects/infinite_effects).
|
||||
|
||||
@ -67,7 +67,8 @@ class MyGame extends BaseGame with TapDetector {
|
||||
|
||||
final sequence = SequenceEffect(
|
||||
effects: [move1, scale, move2, rotate],
|
||||
isInfinite: true, isAlternating: true);
|
||||
isInfinite: true,
|
||||
isAlternating: true);
|
||||
greenSquare.addEffect(sequence);
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import '../position.dart';
|
||||
class SequenceEffect extends PositionComponentEffect {
|
||||
final List<PositionComponentEffect> effects;
|
||||
int _currentIndex = 0;
|
||||
PositionComponentEffect _currentEffect;
|
||||
PositionComponentEffect currentEffect;
|
||||
bool _currentWasAlternating;
|
||||
double _driftModifier = 0.0;
|
||||
|
||||
@ -38,12 +38,14 @@ class SequenceEffect extends PositionComponentEffect {
|
||||
currentSize = Position.fromSize(effect.size);
|
||||
}
|
||||
});
|
||||
travelTime = effects.fold(0, (time, effect) => time + effect.travelTime *
|
||||
(effect.isAlternating ? 2 : 1));
|
||||
travelTime = effects.fold(
|
||||
0,
|
||||
(time, effect) =>
|
||||
time + effect.travelTime * (effect.isAlternating ? 2 : 1));
|
||||
component.setBySize(originalSize);
|
||||
component.setByPosition(originalPosition);
|
||||
_currentEffect = effects.first;
|
||||
_currentWasAlternating = _currentEffect.isAlternating;
|
||||
currentEffect = effects.first;
|
||||
_currentWasAlternating = currentEffect.isAlternating;
|
||||
}
|
||||
|
||||
@override
|
||||
@ -53,11 +55,11 @@ class SequenceEffect extends PositionComponentEffect {
|
||||
}
|
||||
super.update(dt);
|
||||
|
||||
_currentEffect.update(dt + _driftModifier);
|
||||
currentEffect.update(dt + _driftModifier);
|
||||
_driftModifier = 0.0;
|
||||
if (_currentEffect.hasFinished()) {
|
||||
_driftModifier = _currentEffect.driftTime;
|
||||
_currentEffect.isAlternating = _currentWasAlternating;
|
||||
if (currentEffect.hasFinished()) {
|
||||
_driftModifier = currentEffect.driftTime;
|
||||
currentEffect.isAlternating = _currentWasAlternating;
|
||||
_currentIndex++;
|
||||
final iterationSize = isAlternating ? effects.length * 2 : effects.length;
|
||||
if (_currentIndex != 0 && _currentIndex % iterationSize == 0) {
|
||||
@ -66,14 +68,14 @@ class SequenceEffect extends PositionComponentEffect {
|
||||
}
|
||||
final orderedEffects =
|
||||
curveDirection.isNegative ? effects.reversed.toList() : effects;
|
||||
_currentEffect = orderedEffects[_currentIndex % effects.length];
|
||||
_currentWasAlternating = _currentEffect.isAlternating;
|
||||
currentEffect = orderedEffects[_currentIndex % effects.length];
|
||||
_currentWasAlternating = currentEffect.isAlternating;
|
||||
if (isAlternating &&
|
||||
!_currentEffect.isAlternating &&
|
||||
!currentEffect.isAlternating &&
|
||||
curveDirection.isNegative) {
|
||||
// Make the effect go in reverse
|
||||
_currentEffect.isAlternating = true;
|
||||
_currentEffect.percentage = 1.0;
|
||||
currentEffect.isAlternating = true;
|
||||
currentEffect.percentage = 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user