2012-05-13 111 views
0

我有這個代碼應該繪製兩個音量圖標到窗口,但它不工作。下面是相關代碼:XNA沒有繪製我告訴它

Texture2D vol_max; 
    Vector2 vol_max_vect; 
    Texture2D vol_min; 
    Vector2 vol_min_vect; 
    ... 
    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     vol_max = Content.Load<Texture2D>("[email protected]"); 
     vol_min = Content.Load<Texture2D>("[email protected]"); 
    } 
    protected override void Update(GameTime gameTime) 
    { 
     thisKeyboard = Keyboard.GetState(PlayerIndex.One); 

     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || 
      thisKeyboard.IsKeyDown(Keys.Escape)) 
     { 
      this.Exit(); 
     } 

     // Update window vectors 
     vol_max_vect = new Vector2(
      (float)(Window.ClientBounds.Right - 20), 
      (float)(Window.ClientBounds.Bottom - 20)); 
     vol_min_vect = new Vector2(
      (float)(Window.ClientBounds.Right - 140), 
      (float)(Window.ClientBounds.Bottom - 20)); 

     prevKeyboard = thisKeyboard; 

     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 
     spriteBatch.Draw(
      vol_max, 
      vol_max_vect, 
      Color.White); 
     spriteBatch.Draw(
      vol_min, 
      vol_min_vect, 
      Color.White); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 

回答

2

的問題是,ClientBounds.Right /底部是Windows屏幕座標(其中[0,0]是屏幕的左上角,而右下角爲您解析,例如[1024,768])。

你真正想要的是把它們繪製在你自己窗口的右下角。 XNA的SpriteBatch繪製視口座標,其中[0,0]是視口的左上角,右下角是應用程序的分辨率,例如。 [800,480]。爲了獲得這個寬度,可以簡單地使用Window.ClientBounds.Width而不是Window.ClientBounds.Right,而使用Window.ClientBounds.Height而不是Window.ClientBounds.Bottom。

希望有幫助!

+0

是的,它的工作!謝謝! –

相關問題