Skip to main content
GameDev.net gamedev.net
🔒 Locked

Toggle Button Help

Started by Greycrow Apr 1, 2009 at 10:05 AM 1 replies 750+ views
Original Post
Greycrow
Greycrow
I'm trying to implement a simple toggle button to turn on/off the music in my game. The code goes something like this:

if (Mouse.GetState().LeftButton == XButtonState.Pressed)
                {
                    if (playState == (int)musicState.Playing)
                    {
                        playState = (int)musicState.Stopped;
                        cue.Pause();
                    }
                    else
                    {
                        playState = (int)musicState.Playing;
                        cue.Resume();
                    }
                }

The problem here is if you hold the mouse button down over the button it toggles hundreds of times and does not work effectively. I could perhaps fumble my way through it and use a frame timer or something. Does anyone have a code snippet or resource link for doing this so I don't have to re-invent the wheel? Hopefully something I can re-use in future projects. And something as small and as simple as possible. Thanks Grey
Evil Steve
Evil Steve
You want to record the previous mouse button state, and only do anything when the current state is pressed and the state the previous tick was unpressed.

Also, moving to General Programming, since it's not DirectX related.
Greycrow
Greycrow
Thanks that helped:

                if ((Mouse.GetState().LeftButton == XButtonState.Pressed)                 && (oldMouseState != (int)XButtonState.Pressed))                {                    oldMouseState = (int)Mouse.GetState().LeftButton;                    if (playState == (int)musicState.Playing)                    {                        playState = (int)musicState.Stopped;                        cue.Pause();                    }                    else                    {                        playState = (int)musicState.Playing;                        cue.Resume();                    }                }                else                    oldMouseState = (int)Mouse.GetState().LeftButton;


I posted this here because this problem is directly related to XNA
I don't have this problem in regular Windows.Forms

Grey

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.