在我下面的代码中,我认为它是正确完成的。但是idk,_bg调用的图像没有显示出来。屏幕只显示白色。奇怪的是,当我运行它时出现 0 错误消息。
(RenderTarget2D 用于将我的视图默认设置为横向模式)
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GameName2
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
Texture2D _bg;
RenderTarget2D finalImageTarget;
public Game1()
{
_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);
_bg = Content.Load<Texture2D>(@"background");
finalImageTarget = new RenderTarget2D(GraphicsDevice, 1280, 720);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all 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)
{
// 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.SetRenderTarget(finalImageTarget);
GraphicsDevice.Clear(Color.White);
// TODO: Add your drawing code here
_spriteBatch.Begin();
_spriteBatch.Draw(_bg,
new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height),
null,
Color.White,
0,
Vector2.Zero,
SpriteEffects.None,
0);
_spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
_spriteBatch.Begin();
_spriteBatch.Draw(finalImageTarget,
new Vector2(720,0),
null,
Color.White,
MathHelper.PiOver2,
Vector2.Zero,
Vector2.One,
SpriteEffects.None,
0f);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
我使用 xnb 文件。我把它放在项目的内容文件夹中。我已将“内容”和“如果较新则复制”设置为文件属性。
这是我在转换为 xnb 之前使用的 .png 文件:https ://imageshack.us/scaled/large/15/loadingo.png
或者问题出在我的 .png 文件中?
任何想法?谢谢