2012-02-21 81 views
0

當你點擊最上面的按鈕時,它假設在屏幕上繪製一個字符串,但它沒有顯示出來。我移動了mainMenu.UpdateButtons();到Main.cs中的Draw方法,但字符串被繪製,然後再次繪製背景圖像。使其顯示爲字符串出現一瞬間消失。它爲什麼這樣做?XNA C#爲什麼我的字符串不能繪製?

Main.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 
using TestGame.Controls; 
using TestGame.GameStates; 

namespace TestGame 
{ 
public class Main : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    InputHandler inputHandler; 
    public SpriteBatch spriteBatch; 
    public SpriteFont spriteFont; 
    MainMenu mainMenu; 
    Vector2 position; 

    public Main() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 

     inputHandler = new InputHandler(); 
     mainMenu = new MainMenu(this); 

     graphics.PreferredBackBufferWidth = 1280; 
     graphics.PreferredBackBufferHeight = 720; 
    } 

    protected override void Initialize() 
    { 
     this.IsMouseVisible = true; 

     base.Initialize(); 
     mainMenu.MenuInitialize(); 
    } 

    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     spriteFont = Content.Load<SpriteFont>(@"Fonts\MainFont"); 

     mainMenu.MenuLoadContent(); 
    } 

    protected override void UnloadContent() 
    { 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     inputHandler.Update(); 

     if (inputHandler.currentKeyState.IsKeyDown(Keys.Escape)) 
      this.Exit(); 
     mainMenu.frameTime = gameTime.ElapsedGameTime.Milliseconds/1000; 
     MouseState mouseState = Mouse.GetState(); 
     mainMenu.mouseX = mouseState.X; 
     mainMenu.mouseY = mouseState.Y; 
     mainMenu.previouslyPressed = mainMenu.mousePressed; 
     mainMenu.mousePressed = mouseState.LeftButton == ButtonState.Pressed; 
     mainMenu.UpdateButtons(); 
     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.Black); 
     spriteBatch.Begin(); 
     mainMenu.MenuDraw(); 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 
} 

MainMenu.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace TestGame.GameStates 
{ 
public class MainMenu 
{ 
    enum buttonState { hover, up, released, down } 
    const int numberOfButtons = 4, newGameButtonIndex = 0, loadGameButtonIndex = 1, optionsButtonIndex = 2, quitButtonIndex = 3, buttonHeight = 48, buttonWidth = 80; 
    Color[] buttonColor = new Color[numberOfButtons]; 
    Rectangle[] buttonRect = new Rectangle[numberOfButtons]; 
    buttonState[] buttonSt = new buttonState[numberOfButtons]; 
    Texture2D[] buttonTexture = new Texture2D[numberOfButtons]; 
    double[] buttonTimer = new double[numberOfButtons]; 
    public bool mousePressed, previouslyPressed = false; 
    public int mouseX, mouseY; 
    public double frameTime; 
    int buttonPadding; 

    Main main; 
    Texture2D backgroundImage; 
    Texture2D backgroundImageFade; 

    public MainMenu(Game game) 
    { 
     main = (Main)game; 
    } 

    public void MenuInitialize() 
    { 
     for (int i = 0; i < numberOfButtons; i++) 
     { 
      buttonSt[i] = buttonState.up; 
      buttonColor[i] = Color.White; 
      buttonTimer[i] = 0.0; 
      buttonRect[i] = new Rectangle(0, buttonPadding, buttonWidth, buttonHeight); 
      buttonPadding += buttonHeight; 
     } 
    } 

    public void MenuLoadContent() 
    { 
     backgroundImage = main.Content.Load<Texture2D>(@"Backgrounds\titlescreen"); 
     backgroundImageFade = main.Content.Load<Texture2D>(@"Backgrounds\titlescreenfade"); 
     buttonTexture[newGameButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop"); 
     buttonTexture[loadGameButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop"); 
     buttonTexture[optionsButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop"); 
     buttonTexture[quitButtonIndex] = main.Content.Load<Texture2D>(@"Sprites\desktop"); 
    } 

    public void MenuDraw() 
    { 
     main.spriteBatch.Draw(backgroundImage, new Vector2(0, 0), Color.White); 

     for (int i = 0; i < numberOfButtons; i++) 
     { 
      main.spriteBatch.Draw(buttonTexture[i], buttonRect[i], buttonColor[i]); 
     } 
    } 

    Boolean targetImageAlpha(Rectangle rect, Texture2D texture, int x, int y) 
    { 
     return targetImageAlpha(0, 0, texture, texture.Width * (x - rect.X)/rect.Width, texture.Height * (y - rect.Y)/rect.Height); 
    } 

    Boolean targetImageAlpha(float tx, float ty, Texture2D texture, int x, int y) 
    { 
     if (targetImage(tx, ty, texture, x, y)) 
     { 
      uint[] data = new uint[texture.Width * texture.Height]; 
      texture.GetData<uint>(data); 

      if ((x - (int)tx) + (y - (int)ty) * texture.Width < texture.Width * texture.Height) 
      { 
       return ((data[(x - (int)tx) + (y - (int)ty) * texture.Width] & 0xFF000000) >> 24) > 20; 
      } 
     } 
     return false; 
    } 

    Boolean targetImage(float tx, float ty, Texture2D texture, int x, int y) 
    { 
     return (x >= tx && x <= tx + texture.Width && y >= ty && y <= ty + texture.Height); 
    } 

    public void UpdateButtons() 
    { 
     for (int i = 0; i < numberOfButtons; i++) 
     { 
      if (targetImageAlpha(buttonRect[i], buttonTexture[i], mouseX, mouseY)) 
      { 
       buttonTimer[i] = 0.0; 
       if (mousePressed) 
       { 
        buttonSt[i] = buttonState.down; 
        buttonColor[i] = Color.Blue; 
       } 
       else if (!mousePressed && previouslyPressed) 
       { 
        if (buttonSt[i] == buttonState.down) 
        { 
         buttonSt[i] = buttonState.released; 
        } 
       } 
       else 
       { 
        buttonSt[i] = buttonState.hover; 
        buttonColor[i] = Color.LightBlue; 
       } 
      } 
      else 
      { 
       buttonSt[i] = buttonState.up; 

       if (buttonTimer[i] > 0) 
       { 
        buttonTimer[i] = buttonTimer[i] - frameTime; 
       } 
       else 
       { 
        buttonColor[i] = Color.White; 
       } 
      } 

      if (buttonSt[i] == buttonState.released) 
      { 
       onButtonClick(i); 
      } 
     } 
    } 

    void onButtonClick(int i) 
    { 
     switch (i) 
     { 
      case newGameButtonIndex: 
       main.spriteBatch.Begin(); 
       //main.spriteBatch.DrawString(main.spriteFont, "Creating new game", new Vector2(100, 200), Color.White); 
       main.spriteBatch.Draw(backgroundImageFade, new Vector2(0, 0), Color.White); 
       main.spriteBatch.End(); 
       break; 
      default: 
       break; 
     } 

    } 
} 
} 

回答

1

它正在繪製的,但這樣就可以進行刪除它在你的繪圖方法。當您開始將您的繪圖代碼與您的更新代碼混合時,這就是您會遇到的問題。

所以這裏有一個例子,你現在在遊戲中發生了什麼。

  1. 更新主要遊戲
  2. 更新按鈕
  3. 繪製主要遊戲
  4. 抽獎按鈕

則發生點擊這裏發生了什麼。

  1. 更新主要遊戲
  2. 更新按鈕
  3. onButtonClick - >這是你畫你的文字
  4. 抽獎主要遊戲 - >現在清屏和你畫你的按鈕
  5. 抽獎按鈕

所以這一切都「工作」,而不是你真正打算如何。您將要分離您的繪圖代碼,以便您從Draw方法調用進行繪製。基本上檢查是否buttonState已經在你的Draw方法中「釋放」,然後繪製你想要的文本。

+0

我應該檢查按鈕是否已經在我的Main Draw方法或MainMenu中被釋放? – KeyHeart 2012-02-21 04:03:34

+1

我會檢查MainMenu,只需檢查MainMenu的Draw方法。有助於保持這種封裝。菜單將知道如何繪製自己。 – 2012-02-21 04:13:39

+0

如果(buttonSt [test] == buttonState.released) { main.spriteBatch.DrawString(main.spriteFont,「Creating new game」,new Vector2(100,200),Color.White),我將此添加到MenuDraw 中。 ; } – KeyHeart 2012-02-21 04:37:38

相關問題