John Stockton
John Stockton

Reputation: 312

how to ignore mouse hold on SDL2?

Problem:

i want to change my character's anymation anytime a left mouse button is clicked. If i hold the button, it keeps changing the animation, but i don't need that. I want the animation to be changed only one time. I tried to do something like this:

if (csdl_setup->GetMainEvent()->type == SDL_MOUSEBUTTONDOWN) {
        if (csdl_setup->GetMainEvent()->button.button == SDL_BUTTON_LEFT) {
                LeftMouseClicked = true;
        }
    }
    if (LeftMouseClicked == true) {
        LeftMouseClicked = false;
        bob->PlayAnimation(0, 1, 1, 1);
    }

but it doesn't work. Any ideas?

Upvotes: 0

Views: 785

Answers (1)

2501
2501

Reputation: 25753

Use two sets of variables, one for if the button is held or not and the other for whether the button was pressed in the current frame.

LeftMouseClicked = false ;    //is set to false every frame

if (csdl_setup->GetMainEvent()->type == SDL_MOUSEBUTTONDOWN) {
    if (csdl_setup->GetMainEvent()->button.button == SDL_BUTTON_LEFT) {
         if( LeftMouseHeld == false ) {
             LeftMouseClicked = true ;
         }
            LeftMouseHeld = true;
    }
}
if (LeftMouseClicked == true) {
    bob->PlayAnimation(0, 1, 1, 1);
}

Upvotes: 1

Related Questions