![]() You can download the Project for the tutorial from here. Try to re-position the image so that it is drawn in a new location.Ĭan you think of a game you can make using just what you know? Try to modify the code to fade out but not fade back in. Now that you know how to achieve a fade effect, can you think of some places where you might use it? Try to modify the code so the image just fades in but does not fade back out. Now, you can do a quick build to see the Cat Creature fade in and then fade out. We use a little MathHelper method(MathHelper has LOTS of very useful methods) called "Clamp" to keep our AlphaValue within the accept range for Alpha Values (basically 0 to 255).Īnd that's all you need. The last value is the "Alpha" value, that is the value we change from 0 to 255 and from 255 back to 0 to achieve our fade effect. ![]() Those are the Red, Blue and Green values for the color and setting each of them to 255 says we are making white. That's what we create when we say New Color(255, 255, 255. This is how we are achieving the fade effect on the image.īasically, we still have a white color. Particularly, the Color object has changed from the simple Color.White to this new complicated looking Color value. So you will immediately notice that there are some changes to the values being passed into the Draw method of the SpriteBatch object. MBatch.Draw(mCatCreature, new Rectangle(0, 0, mCatCreature.Width, mCatCreature.Height), new Color(255, 255, 255, ( byte)MathHelper.Clamp(mAlphaValue, 0, 255))) So in the "Draw" method, the following line of code: We do that by modifying the line of code that draws the image that we added to the "Draw" method previously. Change the way the image is drawn so that the fade effect is applied. We reverse the direction of the fade by taking the Increment object and multiplying it by -1. ![]() If the Alpha Value object has exceeded these values, then we will reverse the direction of the fade. If enough of a delay has elapsed (the delay tracking object is now less than or equal to zero), then we first reset the delay object and then Increment/Decrement the Alpha value to increase/decrease the amount of fade the image has.įinally, we check to see if the Alpha Value has increased or decreased outside the range of accepted alpha values (colors go from 0-255). Next we check to see if it's time to change the Fade effect. So by only changing the fade every certain interval, the effect can be seen properly. The image would fade in and out too quickly for the viewer to appreciate the effect. If we faded the image every time the Update function was called. This object is used to control how often the object is faded. We start by decrementing the Fade delay tracking object. This code is the meat and potatoes of the logic for fading the image in and out. Protected override void LoadGraphicsContent( bool loadAllContent)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |