Could not load asset as a non-content file
Asked Answered
V

4

5

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.

Vaporize answered 13/8, 2014 at 1:6 Comment(3)
"My team wanting me to become advance programmer" - and you expect us to do the work for you, is that it?Thetis
I don't understand why this won't work. I physically don't understand. Why doesn't the code work? It suppose to put a sprite on the screen,but it create a bugVaporize
Did you bother searching for the answer yourself? This exact question has been answered plenty of times before.Ligni
M
7

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.

Maure answered 25/8, 2014 at 22:7 Comment(0)
R
5

Your game should have a "Content" directory in the project. In that directory you should put your content assets, like the images - BlueAnvil.png. Then you should have a setting in the game ctor where you set the content directory to be "Content":

Content.RootDirectory = "Content";

After that in the LoadContent() method of your game, you must load the asset: Content.Load<Texture2D>("Character/BlueAnvil") and it should pull in your texture provided that you have set the BlueAnvil file to be content as noted above. XS should do an "optimizing PNGs" step when you build the project.

The content folder ends up in the resource bundle which will extract and from which it creates the Texture2D object you requested.

Roughandready answered 13/8, 2014 at 12:48 Comment(0)
E
4

I had this problem for a single jpg file that was in my content folder. I changed the "copy to output directory" properties for that file to "Copy if newer" right click picture - properties

Extraction answered 4/7, 2015 at 14:3 Comment(0)
T
-1

I see one problem in the code. The string construction for the path variable includes a forward slash:

path = "Character/BlueAnvil"

Revise:

path = "Character//BlueAnvil"

or

path = @"Character/BlueAnvil"
Tibetoburman answered 25/8, 2016 at 23:37 Comment(1)
This is not needed. Forward slashes do not have special meaning in C# string constants. If the path included a backwards slash, then that would be needed, but that's not what it is.Expectoration

© 2022 - 2024 — McMap. All rights reserved.