Luke Villanueva
Luke Villanueva

Reputation: 2070

Handling fast click in xna game studio

I'm handling click events and I don't mind the lightning speed processing of the update and draw that's why I'm just using a simple button pressed event for handling clicks. But as any other programmers, I've encountered a problem while using this kind of approach, I'm adding scores like score += 100, as you would have guessed, the adding of score is very fast, in just a click I think the score added 200-400. Here's how I do it.

mouseStateCurrent = Mouse.GetState();
                mousePosition = new Point(mouseStateCurrent.X, mouseStateCurrent.Y);
                if (drawPauseMenu == false)
                {
                    if (pauseBtnRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            drawPauseMenu = true;
                            paused = true;
                        }
                    }
                    else if (binRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            binSelected = 1;
                        }

                    }
                    else if (birdBathRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            birdBathSelected = 1;
                        }

                    }
                    else if (bowlRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            bowlSelected = 1;
                        }

                    }
                    else if (cansRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            cansSelected = 1;
                        }

                    }
                    else if (paintsRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            paintsSelected = 1;
                        }

                    }
                    else if (poolRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            poolSelected = 1;
                        }

                    }
                    else if (pothRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            potSelected = 1;
                        }

                    }
                    else if (tiresRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            tiresSelected = 1;

                        }

                    }
                    else if (vasesRec.Contains(mousePosition))
                    {
                        if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
                        {
                            playerScore += 100;
                            vasesSelected = 1;
                        }

                    }
                    mouseStatePrevious = mouseStateCurrent;
                }

Been trying to play with this code and tried doing it this way,

if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{

    if (mouseStateCurrent.LeftButton == ButtonState.Released)
    {
        playerScore += 100;
        vasesSelected = 1;
    }
}

Still no luck with this. Any ideas? Thanks!

Upvotes: 0

Views: 118

Answers (1)

Nolonar
Nolonar

Reputation: 6122

You're almost there. You've got a mouseStatePrevious and you're setting it correctly, but you're never using it.

Instead of:

if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{ // Why are you checking if the mouse is pressed AND released?
    if (mouseStateCurrent.LeftButton == ButtonState.Released)
    {
        playerScore += 100;
        vasesSelected = 1;
    }
}

Do this:

if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
    if (mouseStatePrevious.LeftButton == ButtonState.Released)
    {
        playerScore += 100;
        vasesSelected = 1;
    }
}

Upvotes: 1

Related Questions