Monogame不绘制PNG图像 [英] Monogame not drawing png images

查看:0
本文介绍了Monogame不绘制PNG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在单人游戏中将精灵绘制到屏幕上,但不起作用。不会给出任何错误。我已经尝试过其他图像,它对它们起作用。也许这与图片是.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);
        }
    }
}

精灵: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);
        }

照常绘制纹理。

这篇关于Monogame不绘制PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆