diff --git a/doc/flame/effects.md b/doc/flame/effects.md index 53fb93bf0..50c792cd7 100644 --- a/doc/flame/effects.md +++ b/doc/flame/effects.md @@ -535,13 +535,14 @@ Usage example: ```dart final effect = ColorEffect( const Color(0xFF00FF00), - const Offset(0.0, 0.8), EffectController(duration: 1.5), + opacityFrom = 0.2, + opacityTo: 0.8, ); ``` -The `Offset` argument will determine "how much" of the color that will be applied to the component, -in this example the effect will start with 0% and will go up to 80%. +The `opacityFrom` and `opacityTo` arguments will determine "how much" of the color that will be +applied to the component. In this example the effect will start with 20% and will go up to 80%. **Note:** Due to how this effect is implemented, and how Flutter's `ColorFilter` class works, this effect can't be mixed with other `ColorEffect`s, when more than one is added to the component, only diff --git a/doc/flame/examples/lib/color_effect.dart b/doc/flame/examples/lib/color_effect.dart index 71a972d0a..ae04bff2f 100644 --- a/doc/flame/examples/lib/color_effect.dart +++ b/doc/flame/examples/lib/color_effect.dart @@ -18,16 +18,16 @@ class ColorEffectExample extends FlameGame { ember.add( ColorEffect( const Color(0xFF00FF00), - const Offset(0.0, 0.6), EffectController(duration: 1.5), + opacityTo: 0.6, ), ); } else { ember.add( ColorEffect( const Color(0xFF1039DB), - const Offset(0.0, 0.6), EffectController(duration: 1.5), + opacityTo: 0.6, ), ); } diff --git a/examples/lib/stories/collision_detection/multiple_worlds_example.dart b/examples/lib/stories/collision_detection/multiple_worlds_example.dart index 2994d35b6..a6bd6e512 100644 --- a/examples/lib/stories/collision_detection/multiple_worlds_example.dart +++ b/examples/lib/stories/collision_detection/multiple_worlds_example.dart @@ -69,11 +69,11 @@ class CollidableEmber extends Ember with CollisionCallbacks { add( ColorEffect( index < 2 ? Colors.red : Colors.green, - const Offset(0, 0.9), EffectController( duration: 0.2, alternate: true, ), + opacityTo: 0.9, ), ); } diff --git a/examples/lib/stories/effects/color_effect_example.dart b/examples/lib/stories/effects/color_effect_example.dart index 9dc8c52c7..de93f2286 100644 --- a/examples/lib/stories/effects/color_effect_example.dart +++ b/examples/lib/stories/effects/color_effect_example.dart @@ -22,15 +22,13 @@ class ColorEffectExample extends FlameGame with TapDetector { )..add( ColorEffect( Colors.blue, - const Offset( - 0.0, - 0.8, - ), // Means, applies from 0% to 80% of the color EffectController( duration: 1.5, reverseDuration: 1.5, infinite: true, ), + // Means, applies from 0% to 80% of the color + opacityTo: 0.8, ), ), ); diff --git a/examples/lib/stories/effects/dual_effect_removal_example.dart b/examples/lib/stories/effects/dual_effect_removal_example.dart index f196bf147..ab307f90d 100644 --- a/examples/lib/stories/effects/dual_effect_removal_example.dart +++ b/examples/lib/stories/effects/dual_effect_removal_example.dart @@ -33,8 +33,8 @@ class DualEffectRemovalExample extends FlameGame with TapDetector { ); colorEffect = ColorEffect( Colors.blue, - const Offset(0.0, 0.8), colorController, + opacityTo: 0.8, ); mySprite.add(colorEffect); diff --git a/packages/flame/lib/src/effects/color_effect.dart b/packages/flame/lib/src/effects/color_effect.dart index 272238ef8..4fe5cffcd 100644 --- a/packages/flame/lib/src/effects/color_effect.dart +++ b/packages/flame/lib/src/effects/color_effect.dart @@ -18,11 +18,19 @@ class ColorEffect extends ComponentEffect { ColorEffect( this.color, - Offset offset, EffectController controller, { + double opacityFrom = 0, + double opacityTo = 1, this.paintId, void Function()? onComplete, - }) : _tween = Tween(begin: offset.dx, end: offset.dy), + }) : assert( + opacityFrom >= 0 && + opacityFrom <= 1 && + opacityTo >= 0 && + opacityTo <= 1, + 'Opacity value should be between 0 and 1', + ), + _tween = Tween(begin: opacityFrom, end: opacityTo), super(controller, onComplete: onComplete); @override diff --git a/packages/flame/test/effects/color_effect_test.dart b/packages/flame/test/effects/color_effect_test.dart index c3bbef2b1..3030adc46 100644 --- a/packages/flame/test/effects/color_effect_test.dart +++ b/packages/flame/test/effects/color_effect_test.dart @@ -13,7 +13,7 @@ void main() { await game.ensureAdd(component); const color = Colors.red; await component.add( - ColorEffect(color, const Offset(0, 1), EffectController(duration: 1)), + ColorEffect(color, EffectController(duration: 1)), ); game.update(0); expect( @@ -43,7 +43,6 @@ void main() { final effect = ColorEffect( color, - const Offset(0, 1), EffectController(duration: 1), ); await component.add(effect); @@ -71,7 +70,6 @@ void main() { final effect = ColorEffect( Colors.red, - const Offset(0, 1), EffectController(duration: 1), ); await component.ensureAdd(effect); @@ -85,5 +83,53 @@ void main() { ); }, ); + + test('Validates opacity values', () { + expect( + () => ColorEffect( + Colors.blue, + EffectController(duration: 1), + opacityTo: 1.1, + ), + throwsAssertionError, + ); + + expect( + () => ColorEffect( + Colors.blue, + EffectController(duration: 1), + opacityFrom: 255, + ), + throwsAssertionError, + ); + + expect( + () => ColorEffect( + Colors.blue, + EffectController(duration: 1), + opacityTo: -254, + ), + throwsAssertionError, + ); + + expect( + () => ColorEffect( + Colors.blue, + EffectController(duration: 1), + opacityFrom: -0.5, + ), + throwsAssertionError, + ); + + expect( + () => ColorEffect( + Colors.blue, + EffectController(duration: 1), + opacityFrom: 0.1, + opacityTo: 0.9, + ), + returnsNormally, + ); + }); }); }