Could not load asset as a non-content file

Help picture Help · Aug 13, 2014 · Viewed 13.6k times · Source

Why does this keep happening? I research it and know of them helps. Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Security.Policy;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Pratice
{
    public class CharacterClass
{
    public Texture2D texture;
    public Vector2 position, velocity;
    public Rectangle SourceRect;
    public string path;
    public bool HasJumped;

    public CharacterClass()
    {
        HasJumped = false;
        position = new Vector2();
        texture = null;
    }

    public void Initialize()
    {

    }

    public void LoadContent(ContentManager Content)
    {
        path = "Character/BlueAnvil";
        texture = Content.Load<Texture2D>(path);
    }

    public void Update(GameTime gameTime)
    {
        position += velocity;

        //input Controls
        KeyboardState keyState = Keyboard.GetState();
        if (keyState.IsKeyDown(Keys.A))
            position.X -= 5f;
        if (keyState.IsKeyDown(Keys.D))
            position.X = 5f;
        if (keyState.IsKeyDown(Keys.Space) && HasJumped == false)
        {
            position.Y -= 10f;
            velocity.Y = -5f;
            HasJumped = true;
        }

        if (HasJumped == true)
        {
            float i = 1;
            velocity.Y += 0.15f*i;
        }

        if (position.Y + texture.Height >= 450)
            HasJumped = false;

        if (HasJumped == false)
            velocity.Y = 0f;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}
}

I need get this fix so I can remember it. Understand to how to do this I'll need help to do it. So I need help, to understand what I'm doing wrong.

Answer

Imperial Justinian picture Imperial Justinian · Aug 26, 2014

As Nahuel Ianni said in his answer, the game loads Content files from the Content Directory. Thus, all content should be placed in the Content directory.

In other words, the actual path it is looking at is "Content/Character/BlueAnvil". Make sure you placed the file in the correct directory.

Other problems that might arise from this is, if you are using Visual Studio, the file may not be being copied to the output. You need to select the file and open properties, and select copy to output and set it either to newer or always.

Finally, there is the file format of the file itself. If it is not an .xnb, then it is unlikely to accepted. .xnb files are created by either XNA or Monogame Content Pipeline Projects. In XNA, all files were and had to be converted to this format, but the Content Pipeline did that for you. In Monogame, there are files that can be loaded straight, but they vary from OS to OS. Windows off the top of my head I can recall accepts both .png and .wav files. I cannot recall the other accepted file formats, and am having trouble finding the handy table I saw last time I was searching.

Thus, what the game is actually looking to load is either "Content/Character/BlueAnvil.xnb" or "Content/Character/BlueAnvil.png"

EDIT: While it has been some time since I posted this answer, and it is still mostly true, I feel I should mention Monogame has now removed the ability for the ContentManager to load non .xnb files, such as .png and .wav. It does, however, have the function to load these files through methods such as Texture2D.FromStream(graphicsDevice, fileStream). Which is what you should use instead.