【问题标题】:Sprite not appearing in monogame?精灵没有出现在单人游戏中?
【发布时间】:2016-03-03 23:56:48
【问题描述】:

我目前正在重新使用 MonoGame,但由于某种原因,我在将简单的精灵绘制到屏幕上时遇到了问题。

我正在使用 VS2015 社区,创建了一个新的多平台 MonoGame 项目,然后右键单击 Content 文件夹并添加现有项目以将我的“player.png”图像加载到项目中。

然后我编写了以下代码来加载图像并绘制到屏幕上:

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

namespace BulletHell
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D playerImage;
    Player player;

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

    /// <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()
    {
        player = new Player(100, 100, playerImage);

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

        // TODO: use this.Content to load your game content here
        playerImage = Content.Load<Texture2D>("player");
        player.Image = playerImage;

    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// game-specific content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <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)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            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)
    {
        GraphicsDevice.Clear(Color.Black);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        spriteBatch.Draw(playerImage, new Vector2(0, 0), Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

知道为什么没有显示精灵吗?

谢谢

【问题讨论】:

    标签: c# visual-studio monogame


    【解决方案1】:

    再次查看您的代码后,我注意到您的 LoadContent 方法中缺少 base.LoadContent();。如果您重新添加它,它应该会绘制它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2016-09-05
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多