Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your main problem is that your AlphaProcess class has low cohesion. It has three main roles, Tweening, Lerping and getting/setting a Sprite Alpha Value. To improve your design, you need to split these into separate decoupled classes.</p> <p><strong>Goal:</strong> Turn your AlphaProcess into a TweenProcess.</p> <p>Instead of passing it a sprite, pass it an interface through which it can get and set the float it wishes to operate on. And instead of calling Lerp directly, pass it an interface or delegate to a Tween function. <em>(I've used an interface instead of a delegate because I've found delegates often create garbage which can cause your game to stutter.)</em></p> <p><strong>The tweening class is:</strong></p> <pre><code>interface IFloatPropertySource { float FloatProperty { get; set; } } interface ITweenFunction { float Tween(float start, float end, float t); } class TweenProcess : GameProcess { float start; IFloatPropertySource floatSource; ITweenFunction tweenFunction; public TweenProcess(IFloatPropertySource floatSource, ITweenFunction tweenFunction) { this.floatSource = floatSource; this.tweenFunction = tweenFunction; } public override void Update(float elapsed) { if (FirstUpdate) { start = floatSource.FloatProperty; FirstUpdate = false; } Time += elapsed; if (Time &gt;= Duration) Finished = true; floatSource.FloatProperty = tweenFunction.Tween(start, end, Time / Duration); } } </code></pre> <p><strong>The class to get/set the alpha is:</strong></p> <pre><code>class SpriteAlphaSource : IFloatPropertySource { Sprite sprite; public SpriteAlphaSource(Sprite sprite) { this.sprite = sprite; } public float FloatProperty { get { return sprite.Alpha; } set { sprite.Alpha = value; } } } </code></pre> <p>I know you're sort of trying to avoid having this class. But all the ways around it, like via reflection are really expensive. In a project that has more renderables than just sprites, you may want to decouple it from sprite and make it operate on a base interface like IRenderable instead.</p> <p><strong>The lerping mechanism is:</strong></p> <pre><code>class Lerp : ITweenFunction { public float Tween(float start, float end, float t) { return MathHelper.Lerp(start, end, t); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload