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

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 68: Line 68:
  
 
[[File:Player xnb file select.png]]
 
[[File:Player xnb file select.png]]
 +
 +
We need to change the properties of the player.xnb file, so right click it and select properties:
 +
 +
[[File:Player xnb properties.png]]
 +
 +
You need to change
 +
 +
[[File:Player xnb properties 2.png]]

Revision as of 13:17, 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().

The graphic for the ship can be downloaded from this link: player.xnb . 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 project folder in the solution explorer and choose Add followed by New Folder and call it Content:

Add new folder.png

Now right click on the folder you just created, and create a new folder called Graphics:

Add new folder 2.png

You can now right click on the graphics folder and Add from an existing folder:

Add file to folder.png

if you browse to the folder you downloaded the player.xnb file to. You will then get a list of files in theat folder and each should have a tick box. Find player.xnb and put a tick in the tick box.

Player xnb file select.png

We need to change the properties of the player.xnb file, so right click it and select properties:

Player xnb properties.png

You need to change

Player xnb properties 2.png