Ahmed Mohsen
Ahmed Mohsen

Reputation: 112

how to prevent repeating function in mouse_click event XNA Gamestudio?

how to prevent repeating function in mouse_click event ?

if (Mouse.GetState().LeftButton == ButtonState.Pressed)
 {
            shoot_sound.Play(); 
            // it plays the sound more than once
            // i'd like to play it just once and stop !
            // how to prevent key repeating 
 }

Upvotes: 0

Views: 113

Answers (2)

Jishan
Jishan

Reputation: 1684

You can check if a complete click has occurred or not by this snippet.

lastMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
if (lastMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
{
    // React to the click of the mouse
    shoot_sound.Play();
}

Upvotes: 1

georgeous
georgeous

Reputation: 45

I don't know the code exactly but I think you want something like this..

// Create an instance of the sound - makes it easier to manage and deal with
SoundEffectInstance sei = yourSound.CreateInstance();

// then when you go to play the sound make sure it isn't playing before platying it...

if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
    if (sei.State == SoundState.Playing)
    {
        yourSound.Play();
    }
}

Sorry for jumping straight into an answer before asking but I don't have enough reputation to just comment so please tell me if I'm confused about your question. If it's the looping you're having issues with, try using an oldMouseState and newMouseState technique to check that you're mouse has been clicked and released before playing the sound. Really hope this helps.

Upvotes: 0

Related Questions