0

嗨,我在 XNA 4/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");

        }
      }
4

1 回答 1

1

解决方案:

需要为 void 的不同调用定义 bool mbOverMouse & bool mbClickMouse,mouseState,previousMouseState。我已经这样做了:

    #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");

        }
    }
于 2013-06-20T19:20:28.997 回答