Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2012, C#, Monogame - load asset exception

I've been fighting with this problems for days now, browsing through the net, yet nothing helped me solve it: I'm creating a MonoGame application on Visual Studio 2012, yet when trying to load a texture I get the following problem:

Could not load Menu/btnPlay asset!

I have set content directory: Content.RootDirectory = "Assets"; Also the file btnPlay.png has properties set: Build Action: Content and Copy to Output directory: Copy if newer.

My constructor and LoadContent functions are totally empty, but have a look yourself:

public WizardGame()
{
    Window.Title = "Just another Wizard game";

    _graphics = new GraphicsDeviceManager(this);

    Content.RootDirectory = "Assets";
}

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    _spriteBatch = new SpriteBatch(GraphicsDevice);

    Texture2D texture = Content.Load<Texture2D>("Menu/btnPlay");

    _graphics.IsFullScreen = true;
    _graphics.ApplyChanges();
}

I would be glad for any help! I'm totally desperate about the problem....

like image 786
user2976693 Avatar asked Nov 10 '13 17:11

user2976693


1 Answers

Under VS2012, Windows 8 64-bits and latest MonoGame as of today (3.0.1) :

  • create a subfolder named Assets
  • set Copy to Output to anything else than Do not copy
  • prepend assets to your texture path when loading it

enter image description here

namespace GameName2
{
    public class Game1 : Game
    {
        private Texture2D _texture2D;
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;

        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
            _texture2D = Content.Load<Texture2D>("assets/snap0009");
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(_texture2D, Vector2.Zero, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

Here's your texture drawn :D

enter image description here

Note:

By convenience I kept the original value that the content's root directory points to : Content.

However, you can also directly specify Assets in the path:

Content.RootDirectory = @"Content\Assets";

Then load your texture without prepending Assets to its path:

_texture2D = Content.Load<Texture2D>("snap0009");
like image 200
aybe Avatar answered Sep 29 '22 18:09

aybe