Failed to load object as file without content

Why is this happening? I will research it and know that it 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 to get this fix so that I can remember it. Understand how to do this, I will need help to do this. Therefore, I need help to understand what I am doing wrong.

+5
source share
4 answers

As Nahuel Ianni said in his answer, the game downloads content files from the content catalog. Thus, all content must be placed in the content directory.

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

Other problems that may arise because of this are, if you are using Visual Studio, the file cannot be copied to the output. You need to select a file and open properties, and then select a copy for output and set it either to a newer one, or always.

Finally, there is the file format of the file itself. If this is not .xnb, then this is unlikely to be accepted .. xnb files are created either by XNA or by the Monogame content pipeline projects. In XNA, all files were and should have been converted to this format, but Content Pipeline did it for you. Monogame has files that can be downloaded directly, but they vary from OS to OS. Windows from my head, which I can remember, accepts .png and .wav files. I can’t remember the other accepted file formats, and it’s hard for me to find a convenient table that I saw the last time I looked. C>

So what the game is really looking for to download is either "Content / Character / BlueAnvil.xnb" or "Content / Character / BlueAnvil.png"

EDIT: Although it has been some time since I posted this answer and it is still mostly true, I believe Monogame has now removed the ability of ContentManager to download .xng files such as .png and .wav. However, it does have a function to load these files using methods such as Texture2D.FromStream (graphicsDevice, fileStream). This is what you should use instead.

+7
source

The project should have a "Content" folder. You should place your content resources in this directory, for example, images - BlueAnvil.png. Then you should have a setting in the ctor game, where you set the content directory "Content":

 Content.RootDirectory = "Content"; 

After that, in the LoadContent() method of your game, you must load the resource: Content.Load<Texture2D>("Character/BlueAnvil") , and it should draw your texture, provided that you have installed the BlueAnvil file as described above. XS must complete the PNG Optimization step when creating the project.

The content folder ends in the resource package, which is , and from which it creates the Texture2D object that you requested.

+5
source

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

+4
source

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

 path = "Character/BlueAnvil" 

Review:

 path = "Character//BlueAnvil" 

or

 path = @"Character/BlueAnvil" 
-1
source

Source: https://habr.com/ru/post/1200172/


All Articles