0

我正在尝试在 Xna 4.0 中制作游戏,但我坚持使用游戏菜单。

    protected override void Update(GameTime gameTime)
    {
        MouseState ms = Mouse.GetState();

        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        if (ms.LeftButton == ButtonState.Pressed && ((ms.X >= 280 && ms.X <= 540) && (ms.Y >= 150 && ms.Y <= 200)))
            current_screen = ScreenState.Difficulty;

        if (current_screen == ScreenState.Title)
        {
            if (ms.LeftButton == ButtonState.Pressed && ((ms.X >= 280 && ms.X <= 540) && (ms.Y >= 360 && ms.Y <= 410)))
                this.Exit();
        }

        if (current_screen == ScreenState.Difficulty)
        {
            if (ms.LeftButton == ButtonState.Pressed && ((ms.X >= 300 && ms.X <= 520) && (ms.Y >= 100 && ms.Y <= 130)))
                current_screen = ScreenState.Title;
            if ((ms.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released) && ((ms.X >= 280 && ms.X <= 540) && (ms.Y >= 150 && ms.Y <= 200)))
            {
                current_screen = ScreenState.MainGameNovice;
            }
        }

        base.Update(gameTime);
        oldMouseState = ms;
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        switch (current_screen)
        {
            case ScreenState.Title:
                spriteBatch.Draw(mybg, GraphicsDevice.Viewport.Bounds, Color.White);
                spriteBatch.Draw(new_game_button, new Vector2(280, 150), Color.White);
                spriteBatch.Draw(load_game_button, new Vector2(280, 220), Color.White);
                spriteBatch.Draw(how_to_play_button, new Vector2(280, 290), Color.White);
                spriteBatch.Draw(quit_game_button, new Vector2(280, 360), Color.White);
                break;

            case ScreenState.Difficulty:
                spriteBatch.Draw(diff_bg, GraphicsDevice.Viewport.Bounds, Color.White);
                spriteBatch.Draw(novice_button, new Vector2(280, 150), Color.White);
                spriteBatch.Draw(inter_button, new Vector2(280, 220), Color.White);
                spriteBatch.Draw(expert_button, new Vector2(280, 290), Color.White);
                break;

            case ScreenState.LoadGame:
                break;

            case ScreenState.HowtoPlay:
                break;

            case ScreenState.MainGameNovice:
                //main game
                spriteBatch.Draw(expert_button, new Vector2(580, 290), Color.White);
                break;

            case ScreenState.MainGameIntermediate:
                ///main game
                break;

            case ScreenState.MainGameExpert:
                ////Main game
                break;
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }

每当我单击新游戏按钮时,它都会显示难度级别。
问题是当我单击新手按钮时,它不会进入看起来像纯蓝屏的屏幕。它通过它,只需几秒钟,它就会回到难度级别菜单。你能帮我么?我在这里被困了大约一天,我已经搜索了类似的问题,但仍然无法正常工作。

4

2 回答 2

1

这是因为您在更新中有两次相同的调用。

看看你的难度陈述:

if (ms.LeftButton == ButtonState.Pressed && ((ms.X >= 280 && ms.X <= 540) && (ms.Y >= 150 && ms.Y <= 200)))
    current_screen = ScreenState.Difficulty;

然后在你的新手游戏声明中:

if ((ms.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released) && ((ms.X >= 280 && ms.X <= 540) && (ms.Y >= 150 && ms.Y <= 200)))
    {
        current_screen = ScreenState.MainGameNovice;

    }

一旦您单击将 current_screen 设置为 MainGameNovice 的按钮,它将立即考虑单击难度按钮,因为没有任何东西阻止它立即运行,因为您没有查看自上次更新以来是否释放了鼠标按钮。这就是你的问题。确保您的难度按钮还包括:

oldMouseState.LeftButton == ButtonState.Released
于 2013-10-17T15:00:54.437 回答
1

您总是必须检查Released鼠标侦听器事件的按下按钮。用这个改变你的代码:

if (ms.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed)

这将确保只检测一次点击。


作为一个建议,不要使用这样的东西:

(ms.X >= 280 && ms.X <= 540) && (ms.Y >= 360 && ms.Y <= 410)

相反,请使用Rectangle结构,它提供了一个有用的方法,称为Rectangle.Contains().

请参阅MSDN

于 2013-10-17T19:57:24.180 回答