无法将资产作为非内容文件加载 [英] Could not load asset as a non-content file

查看:73
本文介绍了无法将资产作为非内容文件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这种情况持续发生?我对其进行了研究,并且知道它们会有所帮助. 代码:

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.

推荐答案

正如Nahuel Ianni在他的回答中所说,游戏从Content Directory加载Content文件.因此,所有内容都应放置在Content目录中.

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.

换句话说,它正在查看的实际路径是"Content/Character/BlueAnvil".确保将文件放置在正确的目录中.

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

由此可能引起的其他问题是,如果您使用的是Visual Studio,则可能未将文件复制到输出中.您需要选择文件并打开属性,然后选择复制以输出并将其设置为较新或始终.

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.

最后,存在文件本身的文件格式.如果不是.xnb,则不太可能接受. .xnb文件是由XNA或Monogame内容管道项目创建的.在XNA中,所有文件都必须并且必须转换为这种格式,但是内容管道为您做到了这一点. 在Monogame中,有些文件可以直接加载,但它们因操作系统而异.我记得我的头顶窗户是接受.png和.wav文件的.我无法回忆起其他可接受的文件格式,并且无法找到上次搜索时看到的便捷表格.

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.

因此,游戏实际要加载的是"Content/Character/BlueAnvil.xnb" 或"Content/Character/BlueAnvil.png"

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

虽然距离我发布此答案已有一段时间了,但大多数情况下它仍然是正确的,我觉得我应该提到Monogame现在已经删除了ContentManager加载非.xnb文件(如.png和.wav.但是,它确实具有通过Texture2D.FromStream(graphicsDevice,fileStream)之类的方法加载这些文件的功能.您应该改用哪个.

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.

这篇关于无法将资产作为非内容文件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆