Monogame不绘制PNG图像图像、Monogame、PNG

由网友(我的未来不是梦)分享简介:我正尝试在单人游戏中将精灵绘制到屏幕上,但不起作用。不会给出任何错误。我已经尝试过其他图像,它对它们起作用。也许这与图片是.png有关?请帮帮我,我想不出来编码:using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;using...

我正尝试在单人游戏中将精灵绘制到屏幕上,但不起作用。不会给出任何错误。我已经尝试过其他图像,它对它们起作用。也许这与图片是.png有关?请帮帮我,我想不出来

编码:

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

namespace Test
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        
        Texture2D realisticSturgeonSprite;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            
            realisticSturgeonSprite = Content.Load<Texture2D>("Sturgeon");
        }

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

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            Texture2D rect = new Texture2D(_graphics.GraphicsDevice, 80, 30);

            Color[] data = new Color[80 * 30];
            for (int i = 0; i < data.Length; i++) data[i] = Color.Chocolate;
            rect.SetData(data);

            Vector2 coor = new Vector2(10, 20);
            _spriteBatch.Begin();
            _spriteBatch.Draw(rect, coor, Color.White);
            _spriteBatch.Draw(realisticSturgeonSprite, new Vector2(50, 60), Color.White);
            _spriteBatch.Draw(sturgeonSprite, new Vector2(80, 90), Color.White);
            _spriteBatch.End();
            

            base.Draw(gameTime);
        }
    }
}
MonoGame如何支持中文显示

精灵:Lake_Sturgeon.png

如果有遗漏的地方,我可以在帖子中添加更多详细信息。

png

先使用Monogame Pipline tool将推荐答案文件处理成xnb文件更方便。最佳跨平台解决方案。

有时您可能需要直接加载PNG。

将现有文件添加到Visual Studio中的项目。选择PNG文件。复制。

在"Visual Studio中的属性"下,将"生成类型"设置为"无"和"如果较新,则将";复制到输出"。

注意:这仅适用于桌面平台。

protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            
            realisticSturgeonSprite = Texture2D.FromFile(GraphicsDevice,@"Lake_Sturgeon.png"); 
  // you can add path if needed (copy to output option above doesn't need it)
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();

            _spriteBatch.Draw(realisticSturgeonSprite, new Vector2(50, 60), Color.White);

            _spriteBatch.End();
           

            base.Draw(gameTime);
        }

照常绘制纹理。

阅读全文

相关推荐

最新文章