4

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: 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 at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

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(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)

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

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

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

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

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

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

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

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

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 TestGame.Game1.LoadContent() in c:\Users\Flame\Documents\Projects\TestGame\TestGame\Game1.cs:0

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

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

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

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

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

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

So what am I doing wrong?

4

5 回答 5

8

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

您可以通过 ctrl 单击文件并按属性在 Xamarin Studio 中调出属性窗口。

您不应包含文件扩展名。

于 2013-07-17T20:24:41.170 回答
2

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

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

于 2013-05-09T21:51:32.580 回答
1

将 Content 文件夹移动到 Assets,并将每个资源文件的“构建操作”设置为“AndroidAsset”,并将“复制到输出目录”设置为“如果较新则复制”。

于 2017-08-08T18:53:24.447 回答
0

就这样用

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

}

代替

texture = Content.Load<Texture2D>("player");
于 2016-12-20T17:04:32.297 回答
-4

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

使用 Xamarin 的简单 2D 游戏

我知道它有点长,但相信我,如果有其他问题,请随时提出:D 问候 Schreda

于 2013-07-16T21:36:56.473 回答