MonoGame中的ContentLoadException [英] ContentLoadException in MonoGame

查看:94
本文介绍了MonoGame中的ContentLoadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Xamarin Studio在MonoGame中加载纹理.我的代码设置如下:

I've been trying load a texture in MonoGame using Xamarin Studio. My code is set up as below :

#region Using Statements
using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;

#endregion

namespace TestGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //Game World
        Texture2D texture;
        Vector2 position = new Vector2(0,0);

        public Game1 ()
        {
            graphics = new GraphicsDeviceManager (this);
            Content.RootDirectory = "Content";              
            graphics.IsFullScreen = false;      
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize ()
        {
            // TODO: Add your initialization logic here
            base.Initialize ();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent ()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch (GraphicsDevice);

            //Content
            texture = Content.Load<Texture2D>("player");
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update (GameTime gameTime)
        {
            // For Mobile devices, this logic will close the Game when the Back button is pressed
            if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
                Exit ();
            }
            // TODO: Add your update logic here         
            base.Update (gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw (GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear (Color.CornflowerBlue);

            //Draw

            spriteBatch.Begin ();
            spriteBatch.Draw (texture, position, Color.White);
            spriteBatch.End ();

            base.Draw (gameTime);
        }
    }
}

当我调试它时,它给我错误:

When I debug it it gives me the error :

Microsoft.Xna.Framework.Content.ContentLoadException:无法加载 播放器资产作为非内容文件! ---> Microsoft.Xna.Framework.Content.ContentLoadException:目录 没找到. ---> System.IO.DirectoryNotFoundException:无法 找到道路的一部分 'C:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ bin \ Debug \ Content \ player.xnb'. ---> System.Exception:

Microsoft.Xna.Framework.Content.ContentLoadException: Could not load player asset as a non-content file! ---> Microsoft.Xna.Framework.Content.ContentLoadException: The directory was not found. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\Flame\Documents\Projects\TestGame\TestGame\bin\Debug\Content\player.xnb'. ---> System.Exception:

-内部异常堆栈跟踪的结尾---

--- End of inner exception stack trace ---

at System.IO .__ Error.WinIOError(Int32 errorCode,String 也许是完整路径)

at at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

在System.IO.FileStream.Init(字符串路径,FileMode模式, FileAccess访问权限,Int32权限,布尔值useRights,FileShare共享, Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs, 字符串msgPath,布尔bFromProxy,布尔useLongPath)

at at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)

at at System.IO.FileStream..ctor(字符串路径,FileMode模式, FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions 选项,字符串msgPath,布尔bFromProxy)

at at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)

at at System.IO.FileStream..ctor(字符串路径,FileMode模式, FileAccess访问,FileShare共享)

at at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

位于System.IO.File.OpenRead(字符串路径)

at at System.IO.File.OpenRead(String path)

位于Microsoft.Xna.Framework.TitleContainer.OpenStream(字符串名称)

at at Microsoft.Xna.Framework.TitleContainer.OpenStream(String name)

在 Microsoft.Xna.Framework.Content.ContentManager.OpenStream(字符串 assetName)

at at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)

-内部异常堆栈跟踪的结尾---

--- End of inner exception stack trace ---

在 Microsoft.Xna.Framework.Content.ContentManager.OpenStream(字符串 assetName)

at at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)

在 Microsoft.Xna.Framework.Content.ContentManager.ReadAsset [T](字符串 assetName,动作`1 recordDisposableObject)

at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)

-内部异常堆栈跟踪的结尾---

--- End of inner exception stack trace ---

在 Microsoft.Xna.Framework.Content.ContentManager.ReadAsset [T](字符串 assetName,动作`1 recordDisposableObject)

at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)

at at Microsoft.Xna.Framework.Content.ContentManager.Load [T](String assetName)

at at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)

在TestGame.Game1.LoadContent()中 c:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Game1.cs:0

at TestGame.Game1.LoadContent() in c:\Users\Flame\Documents\Projects\TestGame\TestGame\Game1.cs:0

位于Microsoft.Xna.Framework.Game.Initialize()

at at Microsoft.Xna.Framework.Game.Initialize()

在TestGame.Game1.Initialize()中 c:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Game1.cs:0

at TestGame.Game1.Initialize() in c:\Users\Flame\Documents\Projects\TestGame\TestGame\Game1.cs:0

位于Microsoft.Xna.Framework.Game.DoInitialize()

at at Microsoft.Xna.Framework.Game.DoInitialize()

在Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)处

at at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)

位于Microsoft.Xna.Framework.Game.Run()

at at Microsoft.Xna.Framework.Game.Run()

在TestGame.Program.Main()中 c:\ Users \ Flame \ Documents \ Projects \ TestGame \ TestGame \ Program.cs:0

at TestGame.Program.Main() in c:\Users\Flame\Documents\Projects\TestGame\TestGame\Program.cs:0

那我在做什么错了?

推荐答案

将png文件的'Build action'设置为'Content',并将'Copy to output directory'设置为'Copy if newer'.

Set the 'Build action' of the png file to 'Content' and set 'Copy to output directory' to 'Copy if newer'.

您可以通过ctrl单击文件并按属性来在Xamarin Studio中打开属性窗口.

You can bring up the properties window in Xamarin Studio by ctrl clicking the file and pressing properties.

您不应包含文件扩展名.

You shouldn't include the file extension.

这篇关于MonoGame中的ContentLoadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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