![]() |
Here is the image I used |
Next go to your Game1.cs file just below where the spritebatch is declared and declare the variables you will need.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Texture2D rocket; | |
Vector2 position; | |
float rotation; | |
float speed; | |
Vector2 mousePosition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rocket = Content.Load<Texture2D>("rocket"); | |
position = new Vector2(200, 200); | |
rotation = 0; | |
speed = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MouseState mouse = Mouse.GetState(); | |
mousePosition = new Vector2(mouse.X, mouse.Y); | |
Vector2 direction = mousePosition - position; | |
direction.Normalize(); | |
rotation = (float)Math.Atan2( | |
(double)direction.Y, | |
(double)direction.X); |
Finally we get to the Draw method. One thing to note here is to make sure you have the origin set to the center of your image. So here I'm dividing the width and height of the image by 2 and setting that to be the origin. It is possible to set the origin to be anywhere on your image and the image should rotate around that origin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spriteBatch.Begin(); | |
spriteBatch.Draw( | |
rocket, | |
position, | |
null, | |
Color.White, | |
rotation, | |
new Vector2( | |
rocket.Width / 2, | |
rocket.Height / 2), | |
1.0f, | |
SpriteEffects.None, | |
1.0f); | |
spriteBatch.End(); |
Now to make your rocket move towards your mouse cursor at a constant speed. Go to the Update method and just after you set the rotation write the following.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
position += direction * speed; |
If you run the project now you should see the rocket follow your mouse cursor no matter where it is.
This is how it should look.
And there ends my first XNA tutorial. If you see any errors or have any questions please leave a comment below.
Wow great blog entry! Saw you have a sexy assed computer today, nice Android sticker on the back! :D
ReplyDeleteExcelent Tutorial!! Thanks!!
ReplyDeleteGreat tutorial, has helped me a lot. Thanks.
ReplyDeleteNice tutorial, ty.
ReplyDelete