2012-02-21 9 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 메서드에서 "릴리스"되고 THEN이 원하는 텍스트를 그리는 지 확인합니다.

+0

버튼이 내 그리기 방법이나 내 MainMenu에서 해제되었는지 확인해야합니까? – KeyHeart

+1

MainMenu를 체크하고, MainMenu의 Draw 메서드를 체크합니다. 모든 방법으로 캡슐화되도록 유지합니다. 메뉴는 그릴 방법을 알게 될 것입니다. –

+0

제가 MenuDraw이 추가되는 경우 (buttonSt [시험] == buttonState.released) { main.spriteBatch.DrawString (main.spriteFont "새로운 게임 생성"새로운 Vector2 (100, 200), Color.White) ; } – KeyHeart

관련 문제