-2
\$\begingroup\$

I want to play a sound when the mouse is within the bounds of a button. When I first did it, it was just a constant sound no stoppages. But I then did it through a SoundEffectInstance, which just repeats the sound over and over while the mouse is within the bounds of the button. I've gotten to play it once. However, say that I would put my mouse over the play button, it would play the sound, but if I put it over the play button again it wouldn't play. So the sound would just play once of each button. I basically just want it to play once if the cursor is over a button. Then play again if the cursor goes over another/same button.

SoundManger class

    static SoundEffect selected;
    static SoundEffectInstance soundEffectInstance;

    public static void Initialize(ContentManager Content) 
    {
        try
        {
            selected = Content.Load<SoundEffect>("Sound Effects/SelectSound");
            soundEffectInstance = selected.CreateInstance();
        }
        catch 
        {
            Debug.Write("SoundManager failed to Initialize");
        }

    }

    public static void PlaySelected() 
    {
        try
        {  
             soundEffectInstance.Play();   
        }
        catch 
        {
            Debug.Write("Selected sound failed");
        }
    }

Update Method for Button.cs

if (btnRect.Contains(mousePosition))
        {
            btnCol.R = 62;
            btnCol.G = 85;
            btnCol.B = 91;

            if (mouseState.LeftButton == ButtonState.Pressed)
             clicked = true;

             SoundManager.PlaySelected();
        }
        else
        {
            btnCol.R = 255;
            btnCol.G = 255;
            btnCol.B = 255;

            clicked = false;
        }
\$\endgroup\$
  • 5
    \$\begingroup\$ I'm betting if you wrote this out on paper or thought about it a little more, you'd be able to figure it out. The button has a played variable, as long as it's true the sound won't play again. So, just reset the played value to false when the mouse leaves the button. \$\endgroup\$ – MichaelHouse Jul 22 '13 at 14:39
  • 1
    \$\begingroup\$ This is a relatively simple answer and I agree with @Byte56 that a very short Google search would have found the answer here. \$\endgroup\$ – Tom 'Blue' Piddock Jul 22 '13 at 16:25
  • \$\begingroup\$ Don't forget to let us know if it has helped you, we always like to hear an update, even on the small questions \$\endgroup\$ – Tom 'Blue' Piddock Jul 25 '13 at 15:34
  • \$\begingroup\$ Sorry dudes, forgot to give you an update. Yeah I managed to get it working, thanks :) \$\endgroup\$ – Dan Scott Jul 27 '13 at 18:37
4
\$\begingroup\$

As per @Byte56's and my comment, this is quite a simple solution even without using any xna sound manager variables. Use a property or variable in your button or whatever handles the button update to flag when the sound is played and whether that sound should be played again.

if (btnRect.Contains(mousePosition))
{
    btnCol.R = 62;
    btnCol.G = 85;
    btnCol.B = 91;

    //set played as true (you can create this variable yourself)
    if (!soundPlayed) {
        SoundManager.PlaySelected();
        soundPlayed = true;
    }

    if (mouseState.LeftButton == ButtonState.Pressed){
        clicked = true;
    }
}
else
{
    btnCol.R = 255;
    btnCol.G = 255;
    btnCol.B = 255;

    played = false; // reset once mouse leaves button
    clicked = false;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.