Adding Content
This file will go over adding content to your game. For help on creating a project please look at Creating a New Project
First of all you are gonna need some content for your game. For this tutorial use the following image of a ball:
Do right-click > Save Image As and save it somewhere with the name "ball.png".
Now open up your game project and look at the left. You should see a solution explorer window. Expand the Content folder and open up Content.mgcb file by double clicking on it.
You should now see a MonoGame Pipeline Tool window open up. In case it didn't get opened, you can right-click on Content.mgcb, select open with and then select MonoGame Pipeline.
Your game content is managed from this external tool. You can add content to your game in one of the following ways:
- Add Existing Item toolbar button
- Edit > Add > Existing Item... menu button
- right-click > Add > Existing Item... context menu
In our case let's use the Add Existing Item toolbar button.
You should now be prompted to select a file. Select the "ball.png" that you have downloaded a moment ago. After that you will be asked on what action you want to do for adding the file. Just leave the it to default and click OK.
Now simply click Save toolbar button and close the tool.
Now that we have added the content, it's time to load it. First declare a new variable so we can load the ball image into memory.
public class Game1 : Game { Texture2D ballTexture; GraphicsDeviceManager graphics;
Next find the LoadContent method and use it to initialize the ballTexture private variable:
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here ballTexture = Content.Load<Texture2D>("ball"); }
And finally, find the Draw method, and let's draw the ball onto the screen:
protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(ballTexture, new Vector2(0, 0), Color.White); spriteBatch.End(); base.Draw(gameTime); }
Now run the game and you should get the following:
For the next part, look at Adding Basic Code page.