鼠标单击问题

时间:2011-07-09 17:00:06

标签: c# xna

我的鼠标点击有问题,这就是我的代码看起来的样子,我的问题是,当我点击某些内容时,它应该转到下一页并绘制其他一些内容,例如当我点击系统图标时它应该进入系统图片屏幕并绘制其行星,当我点击行星它应该在行星表面上,但不幸的是,如果行星坐标与系统相同,当我点击系统时,它会直接进入行星表面。我在更新的开始和结束时使用了许多方法,例如两个鼠标状态,但没有一个工作。可以帮助我吗?

    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);
    }
}

}

1 个答案:

答案 0 :(得分:2)

对于所有鼠标按钮事件,您应该跟踪上一个更新帧的状态,以便(在您的情况下)只有在鼠标按钮最初按下时才能触发菜单事件。因此,您可以使用以下内容代替if (mouse1.LeftButton == ButtonState.Pressed)

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

我建议将此功能包装在自己的类中。


还要考虑用户界面当前标准的基于鼠标的行为是只有当光标位于控件上时按下鼠标按钮时才会激活控件 - 并且仅鼠标释放后