Friday, May 1, 2009

Episode 3 - Moving a sprite using an xbox controller.



Here's the latest video, hit "Read More" for how I did it and the source code.






For this part you will need a wired xbox 360 controller, if you don't have one and would like for me to explain how to do it with the keyboard let me know.

Step 1:

We need a picture, for this I have used a free program called GIMP ( There is a link on the software page <----- ).

For the file format I have chosen .png as it allows for a transparent background.

Step 2:

Declaring the variable is fairly much the same as the previous episode, however this time instead of using a rectangle for the poistioning and size, we are simply declaring a "Vector2" to handle the x and y co-ordinates and we will allow our image to define its own size.

I'm setting the "Vector2" to "Zero" this will place it in the top left corner.

capture3

Step 3:

Loading our content.

This is done exactly the same way as the last episode.

capture2



Step 4:

Let's Draw.

This step is pretty much the same as in the first episode, however instead of using a "Rectangle" we are using a "Vector2".

capture4

Pause......

If you build your project now you will be able to see yur image in the top left corner, you can't move it as yet but we're getting to that.

Step 5:

Move me.

First we need to set up the listener for the game pad using the "GamePadState", then we can use "ThumbStick.Left.X and ThumbStick.Left.Y" to move our character around the screen.

note: The "Left" listed above refers to the thumbstick itself and not the direction pushed. I took way too long working this out.

Now using the line hero_pos.X += GamePadOne.ThumbSticks.Left.X * 5;

we can make our charcter move left and right along the x-axis using the left thumbstick on our controller.

The 5 refers to the speed at which our charcter moves. This will be a variable later. But for now I just wanted to make that badboy move.










move2

Build and go.

Assuming you have done everthing correctly you should now be able to move your character using your xbox controller.



Click here to download source code

0 comments:


Free Blogger Templates by Isnaini Dot Com and Architecture. Powered by Blogger