Monogame 不绘制 png 图像
Monogame not drawing png images
我试图在单人游戏中将精灵绘制到屏幕上,但不起作用。不给出任何错误。我已经尝试过其他图像并且可以与它们一起使用。也许这与图像是 .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
如果有遗漏的内容,我可以在 post 中添加更多详细信息。
使用Monogame Pipline tool先将PNG文件处理成XNB文件更容易。最佳跨平台解决方案。
有时您可能需要直接加载 PNG。
将现有文件添加到 Visual Studio 中的项目。 Select PNG 文件。复制它。
在 Visual Studio 中的属性下,将“构建类型设置为 none”和“如果较新则复制到输出”。
请注意,这仅适用于桌面平台。
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);
}
像往常一样绘制纹理。
我试图在单人游戏中将精灵绘制到屏幕上,但不起作用。不给出任何错误。我已经尝试过其他图像并且可以与它们一起使用。也许这与图像是 .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
如果有遗漏的内容,我可以在 post 中添加更多详细信息。
使用Monogame Pipline tool先将PNG文件处理成XNB文件更容易。最佳跨平台解决方案。
有时您可能需要直接加载 PNG。
将现有文件添加到 Visual Studio 中的项目。 Select PNG 文件。复制它。
在 Visual Studio 中的属性下,将“构建类型设置为 none”和“如果较新则复制到输出”。
请注意,这仅适用于桌面平台。
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);
}
像往常一样绘制纹理。