What's new

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

  • Thread starter GoldBl4d3
  • Start date
  • Views 2,053
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
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:
1194989601543022112rocket.svg.med.png
#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.​
29vnzmr.png
#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.
eikpc.png
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.
2hdxyzt.png
#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.
2ppbp8p.png
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.​
2d17go7.png
#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.​
2pq466q.png
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

Visual Studio

The Original Shiba Inu
Odysseus' Summit Nevar gon' happen in your lifetime Programmer
Messages
2,748
Reaction score
1,488
Points
1,162
Sin$
7
Very nice tutorial I am happy that you are doing this, I am waiting til after the holidays to write my own.
 
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
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.
 
Z

Zerker24

Enthusiast
Messages
945
Reaction score
206
Points
170
Sin$
0
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

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
This error here, and yes I followed the first tutorial.

PcESnL6eEm.png
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

coolbunnymodz

The Original Bunny
Programmer 10th Anniversary Mythical Veteran
Messages
4,386
Reaction score
1,308
Points
650
Sin$
0
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

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
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

coolbunnymodz

The Original Bunny
Programmer 10th Anniversary Mythical Veteran
Messages
4,386
Reaction score
1,308
Points
650
Sin$
0
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

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
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

Ressko

Getting There
Messages
2,025
Reaction score
549
Points
240
Sin$
0
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

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
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
Login
Register