Excalibur icon indicating copy to clipboard operation
Excalibur copied to clipboard

Fast fadeBy action doesn't sometimes complete (probably under stress)

Open spustlik opened this issue 1 year ago • 2 comments

Steps to Reproduce

        for (let i = 0; i < 100; i++) {
            let a = new ex.Actor({
                pos: ex.vec(100, 100),
                opacity: 0,
                width: 20,
                height: 20,
                color: ex.Color.Blue
            });
            this.add(a);
            a.actions
                .fade(1, 100)
                .callMethod(() => console.log('done'));
        }

Expected Result

100x console.log

Actual Result

When using on more actors (100 for example), fade action is not complete, none 'done' in console. When count is less or speed is greater, it is OK.

Environment

  • browsers and versions: Chrome 125.0.6422.78 (64bit)
  • operating system: Windows 11
  • Excalibur versions: v0.30.0-alpha.1068+436d3b1

Current Workaround

slower fadeBy

Motivation

In fact, I am using actions for own particle system with animated sprites and when actions are completed calling cleanup, typically removing particle actor from scene. For sure, actors are childs of another actor, and it is removed after some delay;

spustlik avatar Jun 06 '24 17:06 spustlik

@spustlik Thanks for the issue, definitely a bug. The fade routines are flawed

There are situations where it is possible for this to never be true because of the discrete simulation

image

You might be able to use coroutine in place of the fade action


var newFadeBy = (actor: ex.Actor, fadeChange: number, durationSeconds: number) => {
  // coroutines start automatically
  ex.coroutine(function* () {
    let duration = durationSeconds * 1000; // milliseconds
    let fadeChangeRate = fadeChange / duration;
    let targetOpacity = actor.graphics.opacity + fadeChange;
    while (duration > 0) {
      const elapsed = yield;
      duration -= elapsed;
      actor.graphics.opacity += fadeChangeRate * elapsed;
    }
    actor.graphics.opacity = targetOpacity;
  });
};

actor.onInitialize = () => {
  newFadeBy(actor, -1.0, 2);
};

eonarheim avatar Jun 06 '24 21:06 eonarheim

This issue hasn't had any recent activity lately and is being marked as stale automatically.

github-actions[bot] avatar Aug 06 '24 00:08 github-actions[bot]