2012-02-21 14 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

それが描かれているが、その後、あなたはあなたのDrawメソッドでそれを消去するために進んでください。これは、図面コードと更新コードを混在させ始めるときに得られる問題です。

ここであなたのゲームで今起こっていることの例を示します。

  1. 更新メインゲーム
  2. 更新ボタン
  3. ドローメインゲーム
  4. ドローボタン

クリックが発生し、ここでは何が起こるかです。

  1. 更新メインゲーム
  2. 更新ボタン
  3. onButtonClick - >あなたは
  4. ドローメインゲームあなたのテキストを描画する場所です - >画面が今クリアし、あなたの描くあなたのボタン
  5. ドローボタン

あなたが本当に意図したとおりではありません。 Drawメソッドの呼び出しから描画するように、描画コードを区切りたいと思うでしょう。基本的に、buttonStateがDrawメソッドで「解放」されているかどうかを確認し、次に、必要なテキストを描画します。

+0

メイン描画メソッドまたはメインメニューでボタンが離されているかどうかを確認する必要がありますか? – KeyHeart

+1

MainMenuをチェックインします.MainMenuのDrawメソッドをチェックインしてください。すべての方法でカプセル化された状態に保ちます。メニューは自分自身を描く方法を知っています。 –

+0

これをMenuDrawに追加しました if(buttonSt [test] == buttonState.released) { main.spriteBatch.DrawString(main.spriteFont、 "新しいゲームの作成"、新しいVector2(100,200)、Color.White) ; } – KeyHeart

関連する問題