Skip to content

Camera fadeIn/fadeOut: onComplete sometimes never fires (effect destroyed before its tween ends) #1810

Open

Description

@obiot

What happens

viewport.fadeIn(color, duration, onComplete) sometimes never calls onComplete. Whether it does depends on the frame timing, so it is intermittent. Code that waits for the callback before moving on then stays stuck forever.

Found in the Alex4 example: the outro moves to its next scene in a fadeIn callback. The first scene change worked; the second never happened, with the same code and duration.

Why

FadeEffect.update() sets isComplete as soon as color.alpha >= _targetAlpha. Camera2d.update() then destroys completed effects, and FadeEffect.destroy() stops the tween. But onComplete belongs to the tween, which only fires it once its own elapsed time reaches 1.

Color stores the alpha as a 32-bit float. On the last step before the end, the tween may write a value like 0.99999998, which rounds to 1.0. The effect counts as complete, the camera destroys it, the tween is stopped one step before it would have finished, and onComplete is lost.

Trace of a failing fade (300 ms, 60 fps): the alpha goes up in steps of 0.0556, reaches 1 on frame 18, and the effect is removed on frame 19 without the callback having run.

Suggested fix

Let the tween decide when the fade is over: set isComplete from the tween's own completion, for example in its onComplete wrapper, before calling the user's callback. Alternatively, make sure the user callback runs in destroy() if the tween had not completed.

Workaround

Don't rely on the callback: start the fade and do the next step on a timer of the same duration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions