带有按钮的c# XNA 4菜单不能全部工作

本文关键字:菜单 不能 全部 工作 XNA 按钮 | 更新日期: 2023-09-27 18:19:06

你好,我在xna4/c#中制作一个带有子菜单的主菜单。我创建了一个空白来调用按钮,所有的工作都很好,但在点击按钮只工作的第一个菜单(这在所有的菜单)。

鼠标在所有文本显示上工作,但没有点击(只在所有菜单中的第一个)。我测试了删除释放检查和点击工作在所有,但有释放点击和按下一个菜单的问题…

代码如下:

    #region CheckforMenus
    //Level 0
    public static bool ismenuIntro = true;
    //Level 1
    public static bool ismenuMain = true;
    public static bool ismenuSinglePlayer = false;
    public static bool ismenuMultiplayer = false;
    public static bool ismenuChallenges = false;
    public static bool ismenuSettings = false;
    public static bool ismenuSelectProfile = false;
    public static bool ismenuCredits = false;
    public static bool ismenuExit = false;
    //Level 2
    //public static bool ismenuSinglePlayerP = false;
    public static bool ismenuMultiplayerJoin = false;
    public static bool ismenuMultiplayerHost = false;
    //Level 2 Settings
    public static bool ismenuSVideo = false;
    public static bool ismenuSSound = false;
    public static bool ismenuSControls = false;
    public static bool ismenuSLang = false;
    //Level 3
    public static bool ismenuMultiplayerHostlobby = false;
    #endregion
    public static MouseState previousMouseState;
    public static bool mbOverMouse = false;
    public static bool mbClickMouse = false;
    //The Button
    protected void mButton(SpriteFont mbFont, string mbName, string mbInfo, int mbType, int mbPosX, int mbPosY, string mbOldMenu, string mbNextMenu)
    {
            Vector2 origin1 = Main.fGUIMenu_Small.MeasureString(mbName);
            Rectangle clickableArea = new Rectangle(mbPosX, mbPosY, (int)origin1.X + 100, (int)origin1.Y);

            Point mousePos = new Point(Mouse.GetState().X, Mouse.GetState().Y);
            if (clickableArea.Contains(mousePos))
            {
                if (previousMouseState.LeftButton == ButtonState.Released && Mouse.GetState().LeftButton == ButtonState.Pressed)
                {
                    mbClickMouse = true;
                }
                else
                {
                    mbClickMouse = false;
                }
                mbOverMouse = true;
            }
            else
            {
                mbOverMouse = false;
            }
            spriteBatch.Begin();
            if (mbOverMouse)
            {
                spriteBatch.DrawString(mbFont, mbName, new Vector2(mbPosX, mbPosY), Color.White);
                spriteBatch.DrawString(fGUIMenu_Small, mbName, new Vector2(20, 480), Color.White);
                spriteBatch.DrawString(fGUIMenu_Tiny, mbInfo, new Vector2(20, 500), Color.White);
            }
            else
            {
                spriteBatch.DrawString(mbFont, mbName, new Vector2(mbPosX, mbPosY), Color.White);
                mbOverMouse = false;
            }
            if (mbClickMouse)
            {
                if (mbOldMenu == "ismenuIntro") ismenuIntro = false;
                if (mbOldMenu == "ismenuMain") ismenuMain = false;
                if (mbOldMenu == "ismenuSinglePlayer") ismenuSinglePlayer = false;
                if (mbOldMenu == "ismenuMultiplayer") ismenuMultiplayer = false;
                if (mbOldMenu == "ismenuChallenges") ismenuChallenges = false;
                if (mbOldMenu == "ismenuSettings") ismenuSettings = false;
                if (mbOldMenu == "ismenuSelectProfile") ismenuSelectProfile = false;
                if (mbOldMenu == "ismenuCredits") ismenuCredits = false;
                if (mbOldMenu == "ismenuExit") ismenuExit = false;
                if (mbOldMenu == "ismenuMultiplayerJoin") ismenuMultiplayerJoin = false;
                if (mbOldMenu == "ismenuMultiplayerHost") ismenuMultiplayerHost = false;
                if (mbOldMenu == "ismenuSVideo") ismenuSVideo = false;
                if (mbOldMenu == "ismenuSSound") ismenuSSound = false;
                if (mbOldMenu == "ismenuSControls") ismenuSControls = false;
                if (mbOldMenu == "ismenuSLang") ismenuSLang = false;
                if (mbOldMenu == "ismenuMultiplayerHostlobby") ismenuMultiplayerHostlobby = false;
                if (mbNextMenu == "ismenuIntro") ismenuIntro = true;
                if (mbNextMenu == "ismenuMain") ismenuMain = true;
                if (mbNextMenu == "ismenuSinglePlayer") ismenuSinglePlayer = true;
                if (mbNextMenu == "ismenuMultiplayer") ismenuMultiplayer = true;
                if (mbNextMenu == "ismenuChallenges") ismenuChallenges = true;
                if (mbNextMenu == "ismenuSettings") ismenuSettings = true;
                if (mbNextMenu == "ismenuSelectProfile") ismenuSelectProfile = true;
                if (mbNextMenu == "ismenuCredits") ismenuCredits = true;
                if (mbNextMenu == "ismenuExit") ismenuExit = true;
                if (mbNextMenu == "ismenuMultiplayerJoin") ismenuMultiplayerJoin = true;
                if (mbNextMenu == "ismenuMultiplayerHost") ismenuMultiplayerHost = true;
                if (mbNextMenu == "ismenuSVideo") ismenuSVideo = true;
                if (mbNextMenu == "ismenuSSound") ismenuSSound = true;
                if (mbNextMenu == "ismenuSControls") ismenuSControls = true;
                if (mbNextMenu == "ismenuSLang") ismenuSLang = true;
                if (mbNextMenu == "ismenuMultiplayerHostlobby") ismenuMultiplayerHostlobby = true;
            }
            else
            {
                mbClickMouse = false;
            }

            spriteBatch.End();
            previousMouseState = Mouse.GetState();
    }
    //Here the menus
    //this is called in protected override void Draw(GameTime gameTime) 
    //as this.menuMain();
    protected void menuMain()
    {
        //Main
        //Level 0
        if (ismenuMain)
        {
            mButton(fGUIMenu_Small, "Singleplayer", "test text 1", 0, 20, 80, "ismenuMain", "ismenuSinglePlayer");
            mButton(fGUIMenu_Small, "Multiplayer", "test text 2", 0, 20, 80, "ismenuMain", "ismenuMultiplayer");
            spriteBatch.Begin();
            this.spriteBatch.DrawString(fGUIMenu_AMega, "Main Menu", new Vector2(10, 20), Color.White);
            spriteBatch.End();
        }
        else if (ismenuSinglePlayer)
        {
            mButton(fGUIMenu_Small, "MainMenu1", "hi 1", 0, 20, 80, "ismenuSinglePlayer", "ismenuMain");
            mButton(fGUIMenu_Small, "MainMenu2", "hi 1", 1, 20, 300, "ismenuSinglePlayer", "ismenuMain");
        }
        else if (ismenuMultiplayer)
        {
            mButton(fGUIMenu_Small, "MainMENU", "TEST TEST", 1, 20, 80, "ismenuMultiplayer", "ismenuMain");
        }
      }

带有按钮的c# XNA 4菜单不能全部工作

解决方案:

需要定义bool mbOverMouse &bool mbClickMouse,mouseState,previousMouseState用于不同的void调用。我是这样做的:

    #region CheckforMenus
    //Level 0
    public static bool ismenuIntro = true;
    //Level 1
    public static bool ismenuMain = true;
    public static bool ismenuSinglePlayer = false;
    public static bool ismenuMultiplayer = false;
    public static bool ismenuChallenges = false;
    public static bool ismenuSettings = false;
    public static bool ismenuSelectProfile = false;
    public static bool ismenuCredits = false;
    public static bool ismenuExit = false;
    //Level 2
    public static bool ismenuMultiplayerJoin = false;
    public static bool ismenuMultiplayerHost = false;
    //Level 2 Settings
    public static bool ismenuSVideo = false;
    public static bool ismenuSSound = false;
    public static bool ismenuSControls = false;
    public static bool ismenuSLang = false;
    //Level 3
    public static bool ismenuMultiplayerHostlobby = false;
    #endregion
    //9 bools for 9 different types of buttons, dont need this, only 1 if you like
    bool[] mbOverMouse = new bool[9];
    bool[] mbClickMouse = new bool[9];
    MouseState[] mouseState = new MouseState[9];
    MouseState[] previousMouseState = new MouseState[9];
    void mButtonG(SpriteFont mbFont, string mbName, string mbInfo, int mbType, int mbPosX, int mbPosY, string mbOldMenu, string mbNextMenu)
    {
        int n = new int();
        n = mbType;
        //mbOverMouse[n] = false;
        //mbClickMouse[n] = false;
        Vector2 origin1 = Main.fGUIMenu_Small.MeasureString(mbName);
        Rectangle clickableArea = new Rectangle(mbPosX, mbPosY, (int)origin1.X + 100, (int)origin1.Y);

        Point mousePos = new Point(Mouse.GetState().X, Mouse.GetState().Y);
        if (clickableArea.Contains(mousePos))
        {
            mouseState[n] = Mouse.GetState();
            if (previousMouseState[n].LeftButton == ButtonState.Released && mouseState[n].LeftButton == ButtonState.Pressed)
            //mbType == 1 && mouseStatePrevious1.LeftButton == ButtonState.Released && mouseStateCurrent1.LeftButton == ButtonState.Pressed) //previousMouseState.LeftButton == ButtonState.Released && Mouse.GetState().LeftButton == ButtonState.Pressed
            {
                mbClickMouse[n] = true;
            }
            else
            {
                mbClickMouse[n] = false;
            }
            mbOverMouse[n] = true;
        }
        else
        {
            mbOverMouse[n] = false;
        }
        spriteBatch.Begin();
        if (mbOverMouse[n])
        {
            spriteBatch.DrawString(mbFont, mbName, new Vector2(mbPosX, mbPosY), Color.White);
            spriteBatch.DrawString(fGUIMenu_Small, mbName, new Vector2(20, 480), Color.White);
            spriteBatch.DrawString(fGUIMenu_Tiny, mbInfo, new Vector2(20, 500), Color.White);
        }
        else
        {
            spriteBatch.DrawString(mbFont, mbName, new Vector2(mbPosX, mbPosY), Color.White);
            mbOverMouse[n] = false;
        }
        if (mbClickMouse[n])
        {
            if (mbOldMenu == "ismenuIntro") ismenuIntro = false;
            if (mbOldMenu == "ismenuMain") ismenuMain = false;
            if (mbOldMenu == "ismenuSinglePlayer") ismenuSinglePlayer = false;
            if (mbOldMenu == "ismenuMultiplayer") ismenuMultiplayer = false;
            if (mbOldMenu == "ismenuChallenges") ismenuChallenges = false;
            if (mbOldMenu == "ismenuSettings") ismenuSettings = false;
            if (mbOldMenu == "ismenuSelectProfile") ismenuSelectProfile = false;
            if (mbOldMenu == "ismenuCredits") ismenuCredits = false;
            if (mbOldMenu == "ismenuExit") ismenuExit = false;
            if (mbOldMenu == "ismenuMultiplayerJoin") ismenuMultiplayerJoin = false;
            if (mbOldMenu == "ismenuMultiplayerHost") ismenuMultiplayerHost = false;
            if (mbOldMenu == "ismenuSVideo") ismenuSVideo = false;
            if (mbOldMenu == "ismenuSSound") ismenuSSound = false;
            if (mbOldMenu == "ismenuSControls") ismenuSControls = false;
            if (mbOldMenu == "ismenuSLang") ismenuSLang = false;
            if (mbOldMenu == "ismenuMultiplayerHostlobby") ismenuMultiplayerHostlobby = false;
            if (mbNextMenu == "ismenuIntro") ismenuIntro = true;
            if (mbNextMenu == "ismenuMain") ismenuMain = true;
            if (mbNextMenu == "ismenuSinglePlayer") ismenuSinglePlayer = true;
            if (mbNextMenu == "ismenuMultiplayer") ismenuMultiplayer = true;
            if (mbNextMenu == "ismenuChallenges") ismenuChallenges = true;
            if (mbNextMenu == "ismenuSettings") ismenuSettings = true;
            if (mbNextMenu == "ismenuSelectProfile") ismenuSelectProfile = true;
            if (mbNextMenu == "ismenuCredits") ismenuCredits = true;
            if (mbNextMenu == "ismenuExit") ismenuExit = true;
            if (mbNextMenu == "ismenuMultiplayerJoin") ismenuMultiplayerJoin = true;
            if (mbNextMenu == "ismenuMultiplayerHost") ismenuMultiplayerHost = true;
            if (mbNextMenu == "ismenuSVideo") ismenuSVideo = true;
            if (mbNextMenu == "ismenuSSound") ismenuSSound = true;
            if (mbNextMenu == "ismenuSControls") ismenuSControls = true;
            if (mbNextMenu == "ismenuSLang") ismenuSLang = true;
            if (mbNextMenu == "ismenuMultiplayerHostlobby") ismenuMultiplayerHostlobby = true;
        }
        else
        {
            mbClickMouse[n] = false;
        }

        spriteBatch.End();
        previousMouseState[n] = mouseState[n];
    }
    protected void menuMain()
    {
        if (ismenuMain)
        {
            mbOverMouse[0] = false;
            mbClickMouse[0] = false;
            mButtonG(fGUIMenu_Small, "Singleplayer", "Here my text A", 0, 20, 80, "ismenuMain", "ismenuSinglePlayer");
            mButtonG(fGUIMenu_Small, "Multiplayer", "Here my text B", 0, 20, 110, "ismenuMain", "ismenuMultiplayer");
            spriteBatch.Begin();
            this.spriteBatch.DrawString(fGUIMenu_AMega, "Main Menu", new Vector2(10, 20), Color.White);
            spriteBatch.End();
        }
        else if (ismenuSinglePlayer)
        {
            mbOverMouse[0] = false;
            mbClickMouse[0] = false;
            mButtonG(fGUIMenu_Small, "Singleplayer", "Here my text A", 0, 20, 80, "ismenuSinglePlayer", "ismenuMain");
            mButtonG(fGUIMenu_Small, "Singleplayer", "Here my text B", 0, 20, 300, "ismenuSinglePlayer", "ismenuMain");
        }
        else if (ismenuMultiplayer)
        {
            mbOverMouse[0] = false;
            mbClickMouse[0] = false;
            mButtonG(fGUIMenu_Small, "Multiplayer", "Text", 0, 20, 110, "ismenuMultiplayer", "ismenuMain");
        }
    }