mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +08:00
Add documentation for SequenceEffect
This commit is contained in:
@ -65,6 +65,24 @@ square.addEffect(RotateEffect(
|
|||||||
curve: Curves.easeInOut,
|
curve: Curves.easeInOut,
|
||||||
));
|
));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 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
|
# Examples
|
||||||
|
|
||||||
|
|||||||
@ -67,7 +67,8 @@ class MyGame extends BaseGame with TapDetector {
|
|||||||
|
|
||||||
final sequence = SequenceEffect(
|
final sequence = SequenceEffect(
|
||||||
effects: [move1, scale, move2, rotate],
|
effects: [move1, scale, move2, rotate],
|
||||||
isInfinite: true, isAlternating: true);
|
isInfinite: true,
|
||||||
|
isAlternating: true);
|
||||||
greenSquare.addEffect(sequence);
|
greenSquare.addEffect(sequence);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import '../position.dart';
|
|||||||
class SequenceEffect extends PositionComponentEffect {
|
class SequenceEffect extends PositionComponentEffect {
|
||||||
final List<PositionComponentEffect> effects;
|
final List<PositionComponentEffect> effects;
|
||||||
int _currentIndex = 0;
|
int _currentIndex = 0;
|
||||||
PositionComponentEffect _currentEffect;
|
PositionComponentEffect currentEffect;
|
||||||
bool _currentWasAlternating;
|
bool _currentWasAlternating;
|
||||||
double _driftModifier = 0.0;
|
double _driftModifier = 0.0;
|
||||||
|
|
||||||
@ -38,12 +38,14 @@ class SequenceEffect extends PositionComponentEffect {
|
|||||||
currentSize = Position.fromSize(effect.size);
|
currentSize = Position.fromSize(effect.size);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
travelTime = effects.fold(0, (time, effect) => time + effect.travelTime *
|
travelTime = effects.fold(
|
||||||
(effect.isAlternating ? 2 : 1));
|
0,
|
||||||
|
(time, effect) =>
|
||||||
|
time + effect.travelTime * (effect.isAlternating ? 2 : 1));
|
||||||
component.setBySize(originalSize);
|
component.setBySize(originalSize);
|
||||||
component.setByPosition(originalPosition);
|
component.setByPosition(originalPosition);
|
||||||
_currentEffect = effects.first;
|
currentEffect = effects.first;
|
||||||
_currentWasAlternating = _currentEffect.isAlternating;
|
_currentWasAlternating = currentEffect.isAlternating;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -53,11 +55,11 @@ class SequenceEffect extends PositionComponentEffect {
|
|||||||
}
|
}
|
||||||
super.update(dt);
|
super.update(dt);
|
||||||
|
|
||||||
_currentEffect.update(dt + _driftModifier);
|
currentEffect.update(dt + _driftModifier);
|
||||||
_driftModifier = 0.0;
|
_driftModifier = 0.0;
|
||||||
if (_currentEffect.hasFinished()) {
|
if (currentEffect.hasFinished()) {
|
||||||
_driftModifier = _currentEffect.driftTime;
|
_driftModifier = currentEffect.driftTime;
|
||||||
_currentEffect.isAlternating = _currentWasAlternating;
|
currentEffect.isAlternating = _currentWasAlternating;
|
||||||
_currentIndex++;
|
_currentIndex++;
|
||||||
final iterationSize = isAlternating ? effects.length * 2 : effects.length;
|
final iterationSize = isAlternating ? effects.length * 2 : effects.length;
|
||||||
if (_currentIndex != 0 && _currentIndex % iterationSize == 0) {
|
if (_currentIndex != 0 && _currentIndex % iterationSize == 0) {
|
||||||
@ -66,14 +68,14 @@ class SequenceEffect extends PositionComponentEffect {
|
|||||||
}
|
}
|
||||||
final orderedEffects =
|
final orderedEffects =
|
||||||
curveDirection.isNegative ? effects.reversed.toList() : effects;
|
curveDirection.isNegative ? effects.reversed.toList() : effects;
|
||||||
_currentEffect = orderedEffects[_currentIndex % effects.length];
|
currentEffect = orderedEffects[_currentIndex % effects.length];
|
||||||
_currentWasAlternating = _currentEffect.isAlternating;
|
_currentWasAlternating = currentEffect.isAlternating;
|
||||||
if (isAlternating &&
|
if (isAlternating &&
|
||||||
!_currentEffect.isAlternating &&
|
!currentEffect.isAlternating &&
|
||||||
curveDirection.isNegative) {
|
curveDirection.isNegative) {
|
||||||
// Make the effect go in reverse
|
// Make the effect go in reverse
|
||||||
_currentEffect.isAlternating = true;
|
currentEffect.isAlternating = true;
|
||||||
_currentEffect.percentage = 1.0;
|
currentEffect.percentage = 1.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user