What's new

[XNA 4.0][Series] Content, Input, Draw & Movement

  • Thread starter GoldBl4d3
  • Start date
  • Views 1,692

GoldBl4d3

VIP
VIP
Retired
Messages
3,789
Reaction score
1,440
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.
Skill Level: Easy
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.
 

Visual Studio

The Original Shiba Inu
Messages
2,774
Reaction score
1,486
Very nice tutorial I am happy that you are doing this, I am waiting til after the holidays to write my own.
 

GoldBl4d3

VIP
VIP
Retired
Messages
3,789
Reaction score
1,440
Very nice tutorial I am happy that you are doing this, I am waiting til after the holidays to write my own.
Thanks, I cant wait until others start making tutorials. Lets try to get some game development talk on here.
 

Zerker24

Enthusiast
Messages
947
Reaction score
206
Thanks, I cant wait until others start making tutorials. Lets try to get some game development talk on here.
I can write a file system tutorial if you would like? I just made a good one for a project of mine.
 

Xeren

♦♦♦ God Complex ♦♦♦
Messages
5,689
Reaction score
2,119
The tutorial looks good, hope you continue it.
 

coolbunnymodz

The Original Bunny
Messages
4,384
Reaction score
1,307
Explain what the error is and on what line. Also did you follow the tutorial series from #1.0 ?
This error here, and yes I followed the first tutorial.

 

Xeren

♦♦♦ God Complex ♦♦♦
Messages
5,689
Reaction score
2,119
This error here, and yes I followed the first tutorial.

Try using this:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Xbox360Game1
{
    public class Ship
    {
        public Texture2D ShipTexture;
        public Vector2 Position;
        public Single Rotation;
    }
}
 

coolbunnymodz

The Original Bunny
Messages
4,384
Reaction score
1,307
Try using this:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Xbox360Game1
{
    public class Ship
    {
        public Texture2D ShipTexture;
        public Vector2 Position;
        public Single Rotation;
    }
}
Unfortunately I still get the same error, I'm not a complete noob to this I understand sharp decently well I've never had this error
 

Xeren

♦♦♦ God Complex ♦♦♦
Messages
5,689
Reaction score
2,119
Unfortunately I still get the same error, I'm not a complete noob to this I understand sharp decently well I've never had this error
You could try making it "public static class Ship", and if that doesn't work, then just get rid of "public" in "public Ship ship;". If you need to access it from another class, pass it as a reference.
 

coolbunnymodz

The Original Bunny
Messages
4,384
Reaction score
1,307
You could try making it "public static class Ship", and if that doesn't work, then just get rid of "public" in "public Ship ship;". If you need to access it from another class, pass it as a reference.
I don't think you can make it a static class, it'll return an instance error. I'll try the Ship ship however.

You could try making it "public static class Ship", and if that doesn't work, then just get rid of "public" in "public Ship ship;". If you need to access it from another class, pass it as a reference.
I managed to fix it, changed it from public to private and for some reason it ran lol
 

GoldBl4d3

VIP
VIP
Retired
Messages
3,789
Reaction score
1,440
I managed to fix it, changed it from public to private and for some reason it ran lol
Never go with a temporary fix like that, it will further cause problem in later tutorials. You must have the ship class defined twice in separate classes or something. If you have AIM I can assist you there. You told me what the error is, but not what the actual line of code its pointing to. Tell me that and I can figure it out. But come talk to me on aim and I will team viewer you. Or upload your source and I will send you the corrected version.
 

Ressko

Getting There
Messages
2,027
Reaction score
551
Hey man, I know youre probably not as active on here as you used to be, but I think you should carry on making these tuts. Im planing on making a small game in xna and these tuts are really helpful. Either way, well done on making these.
 

GoldBl4d3

VIP
VIP
Retired
Messages
3,789
Reaction score
1,440
Hey man, I know youre probably not as active on here as you used to be, but I think you should carry on making these tuts. Im planing on making a small game in xna and these tuts are really helpful. Either way, well done on making these.
Thanks for the kind words, but I will not be continuing.
 
Top Bottom