GoldBl4d3
VIP
VIP
Retired
Content, Input, Draw & Movement
In this tutorial we will cover the basics of loading content, retrieving input, and applying that in to movement. This tutorial is for absolute beginners. However, experienced programmers may comment and help others.
Series Part 1: http://www.se7ensins.com/forums/threads/xna-4-0-series-the-project-setup.836045/#post-6161235
Skill Level: Easy
Time to Complete: 20 Minutes
Time to Complete: 20 Minutes
Art Assets Used:
#2.1 Adding Content to Our Project
Ensure you have followed part 1 of this series. Now on the right side is your Solution Explorer. Under the content project, right click Add->Existing Item. Select your rocket ship asset provided above. You will now see your asset in the content project. Name it RocketShip.
#2.2 Loading our Rocket Ship
In our solution explorer, add a new Class to our game project. Name it Ship.cs. Now lets establish some basic principles. Please note to add the using declarations.
Texture2D - Is a flat surface grid of texels.
Vector2 - A vector with two components (X, Y).
Single - A floating point number. Also known as a float.
Go back to our main game class. Here we declare our new ship class and load its content in to memory.
Viewport - Portion of the render target to receive draw calls.
#2.3 Drawing our Rocket Ship
Now, under the Draw method in our main game class. We are going to draw our sprite. First we begin the call, call it, and end the call.
Parameters: (Texture, Position, SourceRectangle, Tint, Rotation, Origin, Scale, Effects, LayerDepth)
SourceRectangle - The portion of the sprite to draw. Use null to draw the entire portion.
Tint - The color overlay. White is full color.
Origin - The location relevant to the sprite that will be used to rotate and position the sprite.
Effects - Determines if the sprite is flipped or not.
LayerDepth - The drawing order. Not used in this circumstances.
Now debug your project. You can do this by clicking the green play arrow in your tool bars. Or go to Debug -> Start Debugging in the menu bar. Or hit F5 on your keyboard.
Your results should be just like this.
#2.4 Retrieving Input & Moving our RocketShip
In the Update method. The most basic way to retrieve input from our keyboard is to get the state of our keyboard once per update call. Per frame, check to see if a button is down. If so, in this example we move the sprite by increasing or decreasing its position. We apply these same principles to rotate the sprite.
Now debug and move the rocket with your keyboard arrows and use the Q and W keys to rotate it.
Thank you for reading and following along. If you have any questions please post below. Please watch out for the next parts in the series. Next we will be talking about how to shoot projectiles from our rocket ship and apply velocity's.