Could not load asset as a non-content file

若如初见. 提交于 2019-12-12 11:15:51

问题


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.


回答1:


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.




回答2:


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 monogamewill extract and from which it creates the Texture2D object you requested.




回答3:


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




回答4:


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"


来源:https://stackoverflow.com/questions/25276539/could-not-load-asset-as-a-non-content-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!