Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Unresponsive SDL Buttons

$
0
0
I am making a game using SDL, on the main menu all my buttons work fine but in the game itself you need to click the buttons a lot (20+ times) before they respond. My button class looks like this:

#include "button.h"

Button::Button( int x, int y, int h, int w)
{
    this->x = x;
    this->y = y;
    this->h = h;
    this->w = w;

}

bool Button::isPressed(SDL_Event *event)
{
    int mouseX = 0, mouseY = 0;

    if(( event->type == SDL_MOUSEBUTTONDOWN )&&( event->button.button == SDL_BUTTON_LEFT ))
    {
        mouseX = event->button.x;
        mouseY = event->button.y;

        if( ( mouseX > x ) && ( mouseX < x + w ) && ( mouseY > y ) && ( mouseY < y + h ) )
        {
            return true;
        }
    }
    return false;
}



and the place I am using it is here:

while(SDL_PollEvent(&event))
            {
                if(option1.isPressed(&event) == true)
                {
                    playerHealth = playerHealth - 5;
                    strengthStat = strengthStat - 1;
                    hadEvent = true;
                    eventRandom = false;
                }

                if(option2.isPressed(&event) == true)
                {
                    testResult = playerCharacter.charismaTest();
                    if(testResult >= 20)
                    {
                        hadEvent = true;
                        eventRandom = false;
                    }
                    else if(testResult > 1)
                    {
                        playerHealth = playerHealth - 5;
                        hadEvent = true;
                        eventRandom = false;
                    }
                    else
                    {
                        playerHealth = playerHealth - 10;
                        playerMoney = playerMoney - 10;
                        hadEvent = true;
                        eventRandom = false;
                    }
                }

                if((option3.isPressed(&event) == true)&&(classChoice == engineer))
                {
                    testResult = playerCharacter.intelligenceTest(5);
                    if(testResult > 15)
                    {
                        hadEvent = true;
                        eventRandom = false;
                    }
                    else
                    {
                        playerHealth = playerHealth - 10;
                        hadEvent = true;
                        eventRandom = false;
                    }
                }
            }



the other places that I use it have the same structure.

Viewing all articles
Browse latest Browse all 51036

Trending Articles