2013-04-28 113 views
1

在我的代碼下面,我認爲它是正確完成的。但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文件?

有什麼想法?謝謝

回答

0
 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(); 

此代碼沒有被繪製到你看到的屏幕上。它正在繪製到finalImageTarget渲染目標。屏幕上不顯示渲染目標是什麼地方。將它設置爲GraphicsDevice.SetRenderTarget(null);會告訴XNA繪製到可見屏幕。

+0

我有,第二.draw – 2013-04-29 05:27:30

1

第二個sprite批處理調用很好奇。它看起來像你正在移動和/或旋轉圖像在屏幕外。

由於您使用的平局this overload你實際上這樣做:

Texture2D texture =   finalImageTarget; 
Vector2 position =   new Vector2(720,0); 
Rectangle? sourceRectangle = null; 
Color color =    Color.White; 
float rotation =    MathHelper.PiOver2; 
Vector2 origin =    Vector2.Zero; 
Vector2 scale =    Vector2.One; 
SpriteEffects effects =  SpriteEffects.None; 
float layerDepth =   0f; 

_spriteBatch.Draw (texture,position,sourceRectangle,color,rotation,origin,scale,effects,layerDepth,layerDepth); 

因此,這意味着你在你的形象定位720個像素屏幕的左上角的右側,然後圍繞其旋轉圖像的左上角爲90度。

我的建議是從簡單開始,按照自己的想法努力工作。使用不帶渲染目標的單個精靈批處理調用,並使用不帶旋轉的源矩形和目標矩形。

編輯:使用調試器來確保所有的變量都與您期望看到的值一起出來。例如,Window.ClientBounds.Width/Height可能會以零形式出現。我想我在移植到Android時看到過這一次,必須將其更改爲使用「視口寬度」和「高度」。

+0

前起初,我沒有使用旋轉(沒有任何rendertarget2d )但結果沒有什麼不同。任何其他想法?謝謝你的解釋。 – 2013-04-29 05:33:39

0

你爲什麼使用Render2D?有沒有特定的原因?

如果沒有的話,我會在你的構造使用

_graphics.PreferredBackBuffer.Width = 720; 
_graphics.PreferredBackbuffer.Height = 1280; 

改變窗口的分辨率。 然後你有你自由放置你的Texture2D圖像使用下面;

_spriteBatch.Begin(); 
     _spriteBatch.Draw(_bg, 
      new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), 
      null, 
      Color.Black, 
      0, 
      Vector2.Zero, 
      SpriteEffects.None, 
      0); 
     _spriteBatch.End(); 

如果您擔心在安置和背景圖像下然後嘗試在您的通話spriteBatch使用圖層。

正如我所說我不知道​​你爲什麼要這樣做,所以我可能是錯的,但這樣做的簡單方法會鍛鍊它是否它的紋理文件或其他東西。

編輯

基於您的評論,ID說的嘗試只是定位的Texture2D通過座標 例如

_spriteBatch.Draw(_bg, new vector2(0,0), color.black); 

_spriteBatch.Draw(_bg, new vector2(screen.width/2,screen.height/2), color.black); 

,如果你仍然無法看到它的ID建議也許查看文件的屬性或文件更改爲測試一個jpg。

+0

我使用rendertexture2d,因爲教程告訴我試試。但正如我剛纔所說,它沒有任何不同 – 2013-04-29 14:51:39