Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Frame Duration being applied before first frame is shown #933

Open
dev-ISO opened this issue Aug 18, 2024 · 0 comments
Open

First Frame Duration being applied before first frame is shown #933

dev-ISO opened this issue Aug 18, 2024 · 0 comments

Comments

@dev-ISO
Copy link

dev-ISO commented Aug 18, 2024

The issue occurs when defining an animation where the first frame has a duration of a few seconds. The AnimationController applies this duration before the first frame is even displayed, causing a noticeable lag when switching from another animation, like "WalkLeft," to the "Idle" animation. This delay results in the first frame of the "Idle" animation not appearing immediately, creating an undesirable pause during the transition.

//Define the "WalkLeft" animation
_spriteSheet.DefineAnimation("WalkLeft", builder =>
{
    builder.IsLooping(true)
        .AddFrame("WalkLeft1", TimeSpan.FromSeconds(0.2))
        .AddFrame("WalkLeft2", TimeSpan.FromSeconds(0.2))
        .AddFrame("WalkLeft3", TimeSpan.FromSeconds(0.2))
        .AddFrame("WalkLeft4", TimeSpan.FromSeconds(0.2));
});

// Define the "Idle" animation
_spriteSheet.DefineAnimation("Idle", builder =>
{
    builder.IsLooping(true)
        .AddFrame("Idle1", TimeSpan.FromSeconds(4))
        .AddFrame("Idle2", TimeSpan.FromSeconds(0.25));
});

I believe the issue stems from the AnimationController's Play() Method

public bool Play(int startingFrame)
{
    if (startingFrame < 0 || startingFrame >= _definition.FrameCount)
    {
        throw new ArgumentOutOfRangeException(nameof(startingFrame), $"{nameof(startingFrame)} cannot be less than zero or greater than or equal to the total number of frames in this {nameof(AnimationController)}");
    }

    //  Cannot play something that is already playing
    if (IsAnimating)
    {
        return false;
    }

    IsAnimating = true;
    _internalFrame = startingFrame;
    CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;
    return true;
}

Because the CurrentFrameTimeRemaining is applied immediately the first frame's duration is being applied before the animation is shown

CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;

To fix this issue I commented out the CurrentFrameTimeRemaining line and set the CurrentFrameTimeRemaining to a TimeSpan of 0 so that the delay happens only after the first frame is rendered to the screen.

//CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;
CurrentFrameTimeRemaining = new TimeSpan(0);

No worries if this is the intended behavior or if I'm not using it correctly, and I apologize if this proposed solution is causing any other issues. I really appreciate all the hard work your team is doing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant