Sign in to follow this  
bigbanggames

XNA Screen Fade Animation

Recommended Posts

bigbanggames    142

I am new to XNA. I am trying to make a basic fade out for some splash screens, credits screen etc. My thought was to bring the opacity value down in increments equal to 1 millisecond. So figuring that XNA tries to keep the fps around 60, I decrement the increment value by the elapsed game time in Millis. The animation is stopping with an opacity of 0.36......  Is the difference the amount of time the draw() method takes?   Plus the time this method takes? What am I missing?  Thanks in advance! 

public string FadeOut(GameTime gameTime, float fadeTimeSeconds)
        {
            float totalTime = fadeTimeSeconds * 1000;
            dTime += gameTime.ElapsedGameTime.Milliseconds;
            tTime += dTime;
            this.Increment = this.Opacity / totalTime;

            if (tTime <= totalTime)
            {
                this.Opacity -= (this.Increment * dTime);
                dTime = 0;
            }

            return this.Opacity.ToString();

        }//end of FadeOut()

 

Share this post


Link to post
Share on other sites
Juliean    7068

The problem most likely lies here:

this.Increment = this.Opacity / totalTime;

You are calculating the increment step over and over again, while the opacity is acutally decreasing. So naturally Increment gets smaller and smaller until it hits 0.0f. Calculate Increment only once when starting the fade OR store the opacity when starting the fade OR use a constant instead of this.Opacity (if you want to fade the game screen it will probably always be 1.0f/255?).

 

Actually, I'd advice you to split this method in two - StartFade and UpdateFade. StartFade would get the fadeTime as a paramter, while UpdateFade would get gameTime as a parameter, and perform the actual fade. You will get precisier results if you, instead of calculating Increment and subing from this.Opacity, like that:

 

this.Opacity = this.OpacityBeforeFade - (this.OpacityBeforeFade) * (this.FadeTime / this.PassedTime);
Edited by The King2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this