在我的代碼下面,我認爲它是正確完成的。但idk,_bg所調用的圖像沒有顯示出來。屏幕只顯示白色。奇怪的是,當我運行它時有0錯誤信息。爲什麼spritebatch.Draw顯示空白輸出?
(該RenderTarget2D是默認-ING我認爲到風景模式)
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文件。我把它放在項目的Content文件夾中。我已經將「內容」和「如果更新」複製到文件屬性中。
這是PNG文件我在使用前轉換成XNB:https://imageshack.us/scaled/large/15/loadingo.png
或者,也許問題出在我的PNG文件?
有什麼想法?謝謝
我有,第二.draw – 2013-04-29 05:27:30