MonoGame中的ContentLoadException

时间:2013-05-09 17:07:07

标签: c# xna monogame

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

#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);
        }
    }
}

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

  

Microsoft.Xna.Framework.Content.ContentLoadException:无法加载   播放器资产作为非内容文件! ---&GT;   Microsoft.Xna.Framework.Content.ContentLoadException:目录   没找到。 ---&GT; System.IO.DirectoryNotFoundException:无法   找到路径的一部分   'C:\用户\火焰\文件\项目\ TestGame \ TestGame \ BIN \调试\内容\ player.xnb'。   ---&GT; System.Exception:

     

---内部异常堆栈跟踪结束---

     

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

     

at System.IO.FileStream.Init(String path,FileMode mode,   FileAccess访问,Int32权限,布尔useRights,FileShare共享,   Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,   String msgPath,Boolean bFromProxy,Boolean useLongPath)

     

at System.IO.FileStream..ctor(String path,FileMode mode,   FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions   options,String msgPath,Boolean bFromProxy)

     

at System.IO.FileStream..ctor(String path,FileMode mode,   FileAccess访问,FileShare共享)

     

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

     

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

     

at at   Microsoft.Xna.Framework.Content.ContentManager.OpenStream(字符串   ASSETNAME)

     

---内部异常堆栈跟踪结束---

     

at at   Microsoft.Xna.Framework.Content.ContentManager.OpenStream(字符串   ASSETNAME)

     

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

     

---内部异常堆栈跟踪结束---

     

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

     

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

     

在TestGame.Game1.LoadContent()中   C:\用户\火焰\文件\项目\ TestGame \ TestGame \ Game1.cs:0

     

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

     

在TestGame.Game1.Initialize()中   C:\用户\火焰\文件\项目\ TestGame \ TestGame \ Game1.cs:0

     

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

     

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

     

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

     

在TestGame.Program.Main()中   C:\用户\火焰\文件\项目\ TestGame \ TestGame \ Program.cs中:0

那么我做错了什么?

5 个答案:

答案 0 :(得分:8)

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

您可以通过按住Ctrl键单击文件并按属性来调出Xamarin Studio中的属性窗口。

您不应包含文件扩展名。

答案 1 :(得分:2)

您需要将文件添加到解决方案的Content目录中,并在属性窗口中将其设置为content / copy(如果较新)。然后在构建过程中将其复制到输出目录。

请注意,您可以使用预编译的XNB文件(通常使用XNA游戏工作室创建),也可以使用原始PNG图像文件。如果您使用后者,则需要在代码中添加文件扩展名。

答案 2 :(得分:0)

只需使用这种方式

using (var stream = TitleContainer.OpenStream ("Content/charactersheet.png"))
{
    characterSheetTexture = Texture2D.FromStream (this.GraphicsDevice, stream);

}

而不是

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

答案 3 :(得分:0)

将内容文件夹移至资产,并为每个资源文件设置&#39;构建操作&#39;到AndroidAsset&#39;并设置&#39;复制到输出目录&#39;到&#39;复制如果更新&#39;。

答案 4 :(得分:-4)

为了让它工作,只需按照我的指示:

Simple 2D game with Xamarin

我知道它有点长,但它确实相信我, 如有其他问题,请随时询问:D 问候 Schreda