1
\$\begingroup\$

I have a problem that not all inputs of my mouse events seem to be registered. The update logic is checking a 2 dimensional array of 10x10 . It's logic for a jewel matching game. So when i switch my jewel I can't click on another jewel for like half a second. I tested it with a click counter variable and it doesn't hit the debugger when i click the second time after the jewel switch. Only if I do the second click after waiting half a second longer. Could it be that the update logic is too heavy that while he is executing update logic my click is happening and he doesn't register it?

What am I not seeing here :)? Or doing wrong. It is my first game.

UPDATE :
I changed to Variable time step, then i saw after the reindex of my jewels (so after the switch) i see the elapsedgametime was 380ms. So I guess that is why he doesn't catch the short "Press" of my mouseclick because update method is still busy with executing the reindexing code. Anyone knows how I can deal with this .. Or do I have to start using threads because my update of reindex takes too long?

SOLVED : The problem was that in my reindexing code I got out of bound exceptions which I catched and then continued. That catching of exceptions caused a massive lag each time a reindex happened. Now everything runs smoothly and I do not have to worry about a slow Update. But I'm still asking the question.. what should you do If you have really heave update logic where the time to process the logic takes almost a 0.5 second? I guess you need to execute your game logic in multiple threads to reduce the update time? I'm also thinking i'll never have to worry about this for a jewels game :p. It's more for a problem for a heavy physics game with alot of game objects ?

My function of the update methode looks like this.

 public void UpdateBoard()
        {
            currentMouseState = Mouse.GetState();
            if (currentMouseState.LeftButton == ButtonState.Pressed &&
                prevMouseState.LeftButton != ButtonState.Pressed)
            {
                debugCount++;
                if (debugCount == 3)
                {
                    int a = 4;
                }

                leftButtonPressed = true;
            }
            if (this.IsBoardActive() == false)
            {
                UpdatingLogic = true;
                if (leftButtonPressed == true)
                {
                    // this.CheckDropJewels(currentMouseState);
                    this.CheckForSwitch(currentMouseState);
                    if (SwitchFound == true)
                    {
                        reIndexSwitchedJewels = true;
                    }
                    this.MarkJewel(currentMouseState);
                }
                if (CheckForMatches == true)
                {
                    if (this.CheckMatches(5) == true)
                    {
                        this.RemoveMatches();
                        reIndexMissingJewels = true;
                    }
                    else
                    {
                        CheckForMatches = false;
                    }
                }

                UpdatingLogic = false;
                if (currentMouseState.RightButton == ButtonState.Pressed &&
                    prevMouseState.RightButton != ButtonState.Pressed)
                {
                    this.CheckMatches(5);
                }

                this.ReIndex();
                leftButtonPressed = false;
            }

            prevMouseState = currentMouseState;
            this.UpdateJewels();

        }
\$\endgroup\$
11
  • \$\begingroup\$ I think you need to include your jewel switching code too. \$\endgroup\$
    – William
    Commented Oct 31, 2013 at 13:31
  • 1
    \$\begingroup\$ when do you run mouse.GetState()? and update the curent and previous states \$\endgroup\$
    – RoughPlace
    Commented Oct 31, 2013 at 14:04
  • \$\begingroup\$ I added the code I currently have, he doesn't always come in the if clause when i click right after a jewel has been switched \$\endgroup\$
    – LordrAider
    Commented Oct 31, 2013 at 15:03
  • \$\begingroup\$ So, prevMouseState is a property of the class, but currentMouseState is created in the function stack? Any specific reason for that? And (related) are you certain that prevMouseState is always valid when you use it in your if statement? \$\endgroup\$ Commented Oct 31, 2013 at 15:49
  • \$\begingroup\$ prevMouseState is a property of my Board class yes. How else can I keep track of the previous state? If it would be created on the function stack I would not know the previous state of my previous update. Or am I seeing this wrong :) \$\endgroup\$
    – LordrAider
    Commented Oct 31, 2013 at 16:28

2 Answers 2

0
\$\begingroup\$

I believe the code could be changed to avoid this problem using two boolean variables to store the button clicks, one for each button. For the left button, could be something like this:

bool isLeftMouseBtnDown = false;

public void UpdateBoard()
{
    // Mouse events processing.
    currentMouseState = Mouse.GetState();
    // If the current state is pressed.
    if (currentMouseState.LeftButton == ButtonState.Pressed)
    {
        // If the left mouse button was already down, then the user is
        // resting or moving the mouse with the button still down, like a drag.
        // Else, then it is a button down event.
        if (isLeftMouseBtnDown)
        {
            mouseDrag();
        }
        else
        {
            mouseDown();
        }
    }
    else
    {
        // If the button is not pressed, but it was before, it means
        // a mouse up event.
        // Else, the user is moving the mouse without pressing any button.
        if (isLeftMouseBtnDown)
        {
            mouseUp();
        }
        else
        {
            mouseMove();
        }
    }

    // Here comes the code that is independent of mouse events.
    if (CheckForMatches == true)
    {
        if (this.CheckMatches(5) == true)
        {
            this.RemoveMatches();
            reIndexMissingJewels = true;
        }
        else
        {
            CheckForMatches = false;
        }
    }

    this.UpdateJewels();
}

public void mouseDown()
{
    // Example code that is dependent on mouse status.
    debugCount++;
    if (debugCount == 3)
    {
        int a = 4;
    }

    if (this.IsBoardActive() == false)
    {
        UpdatingLogic = true;
        // this.CheckDropJewels(currentMouseState);
        this.CheckForSwitch(currentMouseState);
        if (SwitchFound == true)
        {
            reIndexSwitchedJewels = true;
        }
        this.MarkJewel(currentMouseState);
    }

    UpdatingLogic = false;

    this.ReIndex();

    // Sets the mouse button state as down.
    isLeftMouseBtnDown = true;
}

public void mouseUp()
{
    // Sets the mouse button state as up.
     isLeftMouseBtnDown = false;
}
\$\endgroup\$
3
  • \$\begingroup\$ The problem is he never catches the Press because while the press is done he is busy with updating code (reindexing). So the next time the Update method is entered the state is of the button is released. So he never sets it to status pressed. The only way I can solve this I think is if I can capture input at a consistent rate independantly from the update method , because the update method sometimes is too slow. \$\endgroup\$
    – LordrAider
    Commented Nov 1, 2013 at 14:22
  • \$\begingroup\$ I would recommend you to skip some updates instead of reindexing every time. If your code runs at 60 fps, but you only need to reindex every 2 or 3 frames, create a counter for it. AFAIK, there is no other way to track event-like behavior in XNA. Or I would try to optimize the game logic. Can you check what is your current frame rate? \$\endgroup\$
    – Marlos
    Commented Nov 1, 2013 at 16:49
  • 1
    \$\begingroup\$ The optimization got me thinking to update the reindex code and get rid of the lazy catching of out of bound exceptions which made my slow update go again at 60 fps :). So indirectly solved by your tip :p \$\endgroup\$
    – LordrAider
    Commented Nov 2, 2013 at 6:15
0
\$\begingroup\$

not sure when you are running Mouse.GetState() as this is required to update the state of the previous and current mouseState objects

what i would be looking to do is

public void Update(GateTime time)
{
   prevMouseState = currMouseState;
   currMouseState = Mouse.GetState();

   //All Other stuff you want to do 
}

what you code seems to currently be doing is trying to compare two states (that are at the same state) and comparing opposing booleans but you should post more code for us to be sure

\$\endgroup\$
1
  • \$\begingroup\$ edited the code to current code \$\endgroup\$
    – LordrAider
    Commented Oct 31, 2013 at 15:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .