Jump to content

  • Log In with Google      Sign In   
  • Create Account


XNA Screen Fade Animation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 bigbanggames   Members   -  Reputation: 142

Like
0Likes
Like

Posted 16 March 2013 - 11:46 AM

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()

 



Sponsor:

#2 Juliean   GDNet+   -  Reputation: 2454

Like
1Likes
Like

Posted 16 March 2013 - 12:33 PM

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, 16 March 2013 - 12:43 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS