2011-07-09 6 views
0

私はマウスのクリックに問題があります。これは私のコードがどのように見えるかです。私の問題は何かをクリックすると次のページに移動し、システムのアイコンをシステムの画面に移動して惑星を描く必要があります。惑星をクリックすると惑星表面上を移動するはずですが、残念ながら惑星の座標がシステムと同じであれば、システムをクリックすると惑星の表面にまっすぐ進みます。私は更新の開始時と終了時に、例えば2人の親友のために多くの方法を使いましたが、どちらもうまくいきませんでした。すべてのマウスボタンイベントのためにマウスクリックの問題

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; 

    namespace SpaceCraft 
    { 
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Texture2D Button, BG, BG2, title, cursor, SolarBG, PlanetBG; 
    Vector2 cursorpos; 
    Rectangle titlepos, BGpos; 
    SpriteFont Font; 
    SoundEffect MenuSound; 
    SoundEffectInstance MenuSoundInstance; 
    bool MenuIsTrue = true; 
    bool NewGame = false; 
    Random rand = new Random(); 
    MouseState mouse1, mouse2, mouse3, mouse4, mouse5; 
    KeyboardState CurrentKeyboard, LastKeyboard; 
    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     graphics.PreferredBackBufferHeight = 600; 
     graphics.PreferredBackBufferWidth = 800; 
     Content.RootDirectory = "Content"; 
    } 
    TextButton[] menuButtons; 
    Galaxy galaxy; 

    protected override void Initialize() 
    { 


     int systemnumber = rand.Next(2, 8); 
     int planetnumber = rand.Next(3, 10); 
     Player player1 = new Player(); 
     Player player2 = new Player(); 
     int PicNumber = rand.Next(1, 10); 


     galaxy = new Galaxy(systemnumber); 

     SpaceShip[] kships = new SpaceShip[12]; 
     SpaceShip[] rships = new SpaceShip[12]; 
     for (int i = 1; i < 12; i++) 
     { 
      kships[i] = (new SpaceShip(i, i, (i < 6) ? i * 1000 + 1000 : i * i * 1000,   Content.Load<Texture2D>("Pictures/SpaceShips/k" + i.ToString()))); 
      rships[i] = (new SpaceShip(i, i, (i < 6) ? i * 1000 + 1000 : i * i * 1000,   Content.Load<Texture2D>("Pictures/SpaceShips/r" + i.ToString()))); 
     } 

      menuButtons = new TextButton[3]; 
     for (int i = 0; i < 3; i++) 
     { 
      menuButtons[i] = new TextButton(); 
      menuButtons[i].rpos = new Rectangle(200, 100 * i + 270, 400, 80); 
      menuButtons[i].color = Color.LightGray; 
      menuButtons[i].pos = new Vector2(250, 120 * i + 150); 
      menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b1"); 
     } 

     menuButtons[0].name = "New Game"; 
     menuButtons[1].name = " Options"; 
     menuButtons[2].name = " Exit"; 

     BGpos = new Rectangle(0, 0, 800, 600); 
     titlepos = new Rectangle(100, 50, 593, 133); 
     cursor = Content.Load<Texture2D>("Pictures/buttons/Pointer"); 

      base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     PlanetBG = Content.Load<Texture2D>("Pictures/Planets/planetbg"); 
     SolarBG = Content.Load<Texture2D>("Pictures/Planets/space"); 
     BG = Content.Load<Texture2D>("Pictures/Planets/Galaxy"); 
     BG2 = Content.Load<Texture2D>("Pictures/Planets/Galaxy2"); 
     title = Content.Load<Texture2D>("Pictures/buttons/title"); 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     Font = Content.Load<SpriteFont>("Fonts/scorefont"); 
     Button = Content.Load<Texture2D>("Pictures/Buttons/b1"); 
     MenuSound = Content.Load<SoundEffect>("Sounds/Star Wars"); 
     MenuSoundInstance = MenuSound.CreateInstance(); 
     MenuSoundInstance.IsLooped = true; 
     MenuSoundInstance.Play(); 
     for(int i=0;i<galaxy.systems.Length;i++) 
     { 
      galaxy.systems[i].Pic = Content.Load<Texture2D>("Pictures/Planets/solar_system2"); 
      for (int j = 0; j < galaxy.systems[i].planets.Length; j++) 

       galaxy.systems[i].planets[j].Pic = Content.Load<Texture2D>("Pictures/Planets/" + rand.Next(1,10).ToString()); 
      } 
    } 



    protected override void UnloadContent() 
    { 

    } 


    protected override void Update(GameTime gameTime) 
    { 

     mouse1 = Mouse.GetState(); 
     cursorpos = new Vector2(mouse1.X, mouse1.Y); 
     CurrentKeyboard = Keyboard.GetState(); 

     for (int j = 0; j < galaxy.systems.Length; j++) 
      for (int i = 0; i < galaxy.systems[j].planets.Length; i++) 
      { 
       if (galaxy.systems[j].planets[i].Pos.Contains(mouse1.X, mouse1.Y) && mouse1.LeftButton == ButtonState.Pressed && NewGame == false && galaxy.systems[j].SolarIsTrue == false) 
       { 
        galaxy.systems[j].planets[i].PlanetIsTrue = true; 
        galaxy.systems[j].SolarIsTrue = false; 
        NewGame = false; 
        break; 
       } 
      } 

     for (int j = 0; j < galaxy.systems.Length; j++) 
     { 
      if (galaxy.systems[j].Pos.Contains(mouse1.X, mouse1.Y) && mouse1.LeftButton == ButtonState.Pressed && NewGame == true) 
      { 
       galaxy.systems[j].SolarIsTrue = true; 
       break; 
      } 
     } 

     for (int i = 0; i < 3; i++) 
     { 

      if (menuButtons[i].rpos.Contains(mouse1.X, mouse1.Y) && MenuIsTrue == true && NewGame == false) 
      { 

       if (mouse1.LeftButton == ButtonState.Pressed) 
       { 

        if (i == 2 && MenuIsTrue == true) Exit(); 
        if (i == 1 && MenuIsTrue == true) { MenuIsTrue = false; } 
        if (i == 0 && MenuIsTrue == true) { MenuIsTrue = false; MenuSoundInstance.Stop(); NewGame = true; } 
        menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b1"); 
        menuButtons[i].pos = new Vector2(330, 100 * i + 292); 
       } 
       else 
       { 
        menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b2"); 
        menuButtons[i].pos = new Vector2(330, 100 * i + 290); 
       } 
      } 
      else 
      { 
       menuButtons[i].pic = Content.Load<Texture2D>("Pictures/Buttons/b1"); 
       menuButtons[i].pos = new Vector2(330, 100 * i + 292); 
      } 
     } 

      for (int i = 0; i < galaxy.systems.Length; i++) 
      { 
       if (galaxy.systems[i].SolarIsTrue == true && CurrentKeyboard.IsKeyDown(Keys.Escape)&&LastKeyboard.IsKeyUp(Keys.Escape)) 
       { 
        galaxy.systems[i].SolarIsTrue = false; 
        NewGame = true; 
        break; 
       } 
      } 

      for (int j = 0; j < galaxy.systems.Length; j++) 
       if (galaxy.systems[j].SolarIsTrue == true && CurrentKeyboard.IsKeyDown(Keys.Escape) && LastKeyboard.IsKeyUp(Keys.Escape)) 
       { 
        galaxy.systems[j].SolarIsTrue = false; 
        NewGame = true; 
        break; 
       } 
      for (int j = 0; j < galaxy.systems.Length; j++) 
       for (int i = 0; i < galaxy.systems[j].planets.Length; i++) 
       {   
        if (CurrentKeyboard.IsKeyDown(Keys.Escape) && LastKeyboard.IsKeyUp(Keys.Escape) && galaxy.systems[j].planets[i].PlanetIsTrue == true) 
        { 
         galaxy.systems[j].planets[i].PlanetIsTrue = false; 
         galaxy.systems[j].SolarIsTrue = true; 
         break; 
        } 
       } 
      if (CurrentKeyboard.IsKeyDown(Keys.Escape) && LastKeyboard.IsKeyUp(Keys.Escape) && NewGame == true) 
        { 
         MenuIsTrue = true; 
         NewGame = false; 
         MenuSoundInstance.Play(); 
        } 
     LastKeyboard = Keyboard.GetState(); 

     base.Update(gameTime); 
    } 

     protected override void Draw(GameTime gameTime) 
    { 

     GraphicsDevice.Clear(Color.Navy); 
     Random rand = new Random(); 
     spriteBatch.Begin(); 
     spriteBatch.Draw(BG, BGpos, Color.White); 



      if (NewGame) 
      { 
       spriteBatch.Draw(BG2, BGpos, Color.White); 
       for (int i = 0; i < galaxy.systems.Length; i++) 
       { 
        spriteBatch.Draw(galaxy.systems[i].Pic, galaxy.systems[i].Pos, Color.Wheat); 

       } 
      } 

      for (int i = 0; i < galaxy.systems.Length; i++) 
      { 
       if (galaxy.systems[i].SolarIsTrue) 
       { 
        spriteBatch.Draw(SolarBG, new Vector2(0, 0), Color.White); 
        for (int j = 0; j < galaxy.systems[i].planets.Length; j++) 
         spriteBatch.Draw(galaxy.systems[i].planets[j].Pic, galaxy.systems[i].planets[j].Pos, Color.White); 
       } 
      } 


     if(MenuIsTrue) 
     { 
      spriteBatch.Draw(BG, BGpos, Color.White); 
      spriteBatch.Draw(title, titlepos, Color.White * 0.7f); 
      for (int i = 0; i < 3; i++) 
      { 
       spriteBatch.Draw(menuButtons[i].pic, menuButtons[i].rpos, menuButtons[i].color * 0.7f); 
       spriteBatch.DrawString(Font, menuButtons[i].name, menuButtons[i].pos, menuButtons[i].color * 0.7f); 
      } 
     } 
     for (int i = 0; i < galaxy.systems.Length; i++) 
      for (int j = 0; j < galaxy.systems[i].planets.Length; j++) 
       if (galaxy.systems[i].planets[j].PlanetIsTrue == true) 
       { 
        spriteBatch.Draw(PlanetBG, new Vector2(0, 0), Color.White); 
       } 
        MouseState mouse = Mouse.GetState(); 
        spriteBatch.Draw(cursor, cursorpos, Color.White); 

     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 

}

答えて

2

マウスボタンがある場合にのみ、(あなたのケースで)あなたがメニューイベントを起動できるように、あなたは、以前の更新フレームの状態を追跡するべきである最初はが押されています。

if (mouse1.LeftButton == ButtonState.Pressed && prevMouse1.LeftButton != ButtonState.Pressed) 
{ 
    // ... 
} 
prevMouse1 = mouse1; // Update the state for the next frame. 

は私が独自のクラスでこの機能をラップ推薦:だから、代わりにif (mouse1.LeftButton == ButtonState.Pressed)のは、次のようなものを持っているでしょう。そして後にのみ -


は、ユーザインターフェースのための現在の標準的なマウスベースの動作は、マウスボタンが両方カーソルがコントロール上にあるときに放出が押下された場合にのみ制御が活性化されることがあることを考えますマウスはをリリースしました。

関連する問題