Difference between revisions of "Creating An Instance of Your Player"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 54: Line 54:
  
 
Now we need to create a folder for Content and within that a folder for Graphics, you can do this by right clicking on the
 
Now we need to create a folder for Content and within that a folder for Graphics, you can do this by right clicking on the
 +
 +
[[File:Add new folder.png]]
 +
 +
Now right click on the folder you just created:
 +
 +
[[File:Add new folder 2.png]]

Revision as of 12:43, 18 March 2017

You should at this stage have a Player class created and defined. The next stage is to create an instance of the class (the class is a definition similar to a class of Human, you are an instance of the class human).

Create a Player

In order to be able to use the Player class we need to add the code Player player; into the using section of your Game1.cs file. It must go between the public class Game1:Game line and the public Game1() line:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Player player;

    public Game1()

The Initialize method is called immediately before your game is run. The code above has declared an instance of Player, to create the instance we need to add the code player = new Player(); into the Initialize method of Game1.cs:

protected override void Initialize()
{
    // TODO: Add your initialization logic here
    // Initialize the player class
    player = new Player();
    base.Initialize();
}

The LoadContent method is run once during your game, and it will be once once the game is started (but after Initialize).

This method should be used to load your the games contents. Add the following code after the spriteBatch = new SpriteBatch(GraphicsDevice); line:

// Load the player resources
Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
player.Initialize(Content.Load<Texture2D>("Graphics\\player"), playerPosition);

The Draw method is called when your game needs to draw itself. Add the code below into the Draw method:

// Start drawing
_spriteBatch.Begin();

// Draw the Player
player.Draw(_spriteBatch);

// Stop drawing
_spriteBatch.End();

Any drawing to the screen should now be placed between the Begin() and End().

Now we need to create a folder for Content and within that a folder for Graphics, you can do this by right clicking on the

Add new folder.png

Now right click on the folder you just created:

Add new folder 2.png