【发布时间】:2021-12-05 04:28:49
【问题描述】:
我正在尝试在单人游戏中将精灵绘制到屏幕上,但不起作用。不给出任何错误。我尝试了其他图像,它适用于它们。也许它与.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);
}
}
}
如果我遗漏了任何内容,我可以在帖子中添加更多详细信息。
【问题讨论】:
-
文件名不应该以
.png结尾吗? -
-
@Steven,通过内容管理器加载文件的扩展名是
.xnb,必须省略。