将 png 绘制到 SpriteBatch 不起作用

本文关键字:SpriteBatch 不起作用 绘制 png | 更新日期: 2023-09-27 18:33:32

我刚刚开始研究MonoGame,我只是想在Game类中加载简单的Sprite,如下所示:

public class Main : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D test;
        public Main()
        {
            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()
        {
            // 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);
            // TODO: use this.Content to load your game content here
            test =  Content.Load<Texture2D>("default");
        }
        /// <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.CornflowerBlue);
            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(test, Vector2.Zero, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }

但是它不起作用,这里缺少什么?

将 png 绘制到 SpriteBatch 不起作用

我认为

这是因为您将类传递给SpriteBatch(GraphicsDevice device)构造函数而不是它的实例。

尝试将spriteBatch = new SpriteBatch(GraphicsDevice);替换为 spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

我也遇到了一些麻烦,让alpha频道在MonoGame中工作,使用spriteBatch.Begin ((SpriteSortMode)0, BlendState.NonPremultiplied)对我有用。

通常,您需要指定要加载的图像文件类型,请尝试在"默认"的末尾添加".png"。因为您说图像正在正确加载,这可能无法解决您的问题,但我建议您尝试一下。

如果这不起作用,则问题可能出在您的操作系统以及Monogame和/或Visual Studio的版本上;我遇到了图像未绘制的问题,但其他所有内容都在Visual Studio中使用冲突的版本和设置进行编译。希望对您有所帮助!