Troubleshooting this TutorialSometimes, even though you try hard to understand the information in a tutorial, things don't work out quite like you want it to. This page is here to help you resolve any problems you might be having with the tutorial on rotating sprites in MonoGame. The Common Mistakes section describes common problems that people have when doing the things in this tutorial, and how to resolve them. The Frequently Asked Questions section describes questions that people have that aren't related to mistakes, but rather, trying to understand the stuff better or exploring how it can be used. If your problem or concern isn't addressed here, feel free to add a comment below, so that I know where you're having trouble. I like to keep these pages fairly clean, so I may remove comments that I felt like have been addressed. If I remove your comment and you don't feel like the problem has been fixed, repost the question and we'll take another look at it. If a tutorial has a mistake in it, I will fix the mistake and reply to the comment with a brief explanation. However, after a couple of weeks I'll likely go back and remove the original comment as well as my reply, because, hopefully, the problem will have been fixed, and it won't be a concern any more. |
Common MistakesNone listed yet… |
Frequently Asked QuestionsNone listed yet… |
MonoGame says this particular Draw method requires a destination rectangle and source rectangle, but none of your tutorials cover how to use them.
When I run the code I get a exception of type "system.NullReferenceException" the additional info is Object reference not
set to an instance of the object. It says the statement that will be excecuted next is the line that says:
Rectangle sourceRectangle = new Rectangle(0, 0, arrow.Width, arrow.Height);
I must have done something wrong with that line.
I did a little testing and I found out that I also get a exception if I try to do:
spriteBatch.Draw(arrow, new Rectangle(0, 0, 800, 400), Color.White);
The exception for that line is: System.ArgumentNullException occurred in MonoGame.Framework.dll
Additional info: Value cannot be null
Hi, I would like to know how to add a rotation to this animated sprite.
Rotation is a fairly trivial matter. You'll need to make 3 notable changes to your code.
First, you will need a variable to track the angle of rotation. It's very important to keep in mind that this variable has to be converted to radians. For the time being consider the Angle float as measured in degrees, the conversion will come during the rendering.
Second off, you will need to determine the Origin for the rotational axis. For most purposes, this is going to be the center of the texture image you want to rotate. We'll add that as a Vector2 in our variable declarations and load it with a default value of (0,0):
After Load<Texture2D> has been implemented, you will need to make the following calculation:
In this particular code, TextureName is the Texture2D you want to rotate.
And finally, you will need to modify your rendering Draw method to include the Angle float variable for rotation. XNA and your computer hardware takes care of all the hard work. There are 7 flavors of the Draw method, and you're going to want either option 6 or option 7. The following code assumes you're using option 6 instead of option 7. The only difference in these two calls is that in option 6 the Scale parameter utilizes a float. This allows you to scale in a uniform manner. Option 7, on the other hand, utilizes a Vector2 for the Scale parameter. That would allow you to scale on both axis, or in other words: stretch it wider than you can taller, and vice versa instead of the Scale maintaining a square 'box' shape. Again, TextureName is the Texture2D you want to draw. Origin is a Vector2 that represents the center of the image. The final calculation for converting degrees to radians is contained in the draw code below. MathHelper.Pi was included in the libraries used to test this code. However, if it doesn't appear, try adjusting your using statements.
"May the mercy of His Divine Shadow fall upon you." - Stanley H. Tweedle, Security Guard class IV, The League of 20,000 planets
Post preview:
Close preview