Order of calling methods - why does it matter in this example?

user2056166

I have the following method, called in my update loop, to check if one of two buttons has been pressed.

    private void CheckInputs()
    {
        if (CheckButton(startNewGameButtonTexture, startNewGameButtonPosition))
        {
           //break 1 is here
        }

        if (CheckButton(continueCareerButtonTexture, continueCareerButtonPosition))
        {
           //break 2 is here
        }
    }

Buttons are just rectangles, and if the mouse is over them, and the click is released the CheckButton bool returns true:

    public bool CheckButton(Texture2D texture, Vector2 vector)
    {
        MouseState CurrentMouseState = Mouse.GetState();
        bool outcome;

        if (CurrentMouseState.X > vector.X && CurrentMouseState.X < vector.X + texture.Width &&
            CurrentMouseState.Y > vector.Y && CurrentMouseState.Y < vector.Y + texture.Height)
        {
            if (CurrentMouseState.LeftButton == ButtonState.Released && PreviousMouseState == ButtonState.Pressed)
            {
                outcome = true;
            }
            else
            {
                outcome = false;
            }
        }
        else
        {
            outcome = false;
        }

        PreviousMouseState = CurrentMouseState.LeftButton;
        return outcome;
    }

In the current order, the startNewGameButton works (ie debugging stops at break 1), but the continueCareerButton doesn't work (clicking on the button does not trigger break 2).

But if I change the order of the the checks to this:

    private void CheckInputs()
    {
        if (CheckButton(continueCareerButtonTexture, continueCareerButtonPosition))
        {
           //break 2 is here
        }

        if (CheckButton(startNewGameButtonTexture, startNewGameButtonPosition))
        {
           //break 1 is here
        }
    }

the continueCareerButton now works (break 2), but the startNewGameButton now doesn't (break 1).

I think the mouse states must not be working properly, but I can't work out why.

Scott Greenup

You are setting your PreviousMouseState every time you check.

You should be setting it once per frame, the simplest way to do this is inside the Update() function:

void Update(..) {
    CurrentMouseState := Mouse.GetState()

    // code...

    PreviousMouseState = CurrentMouseState
}

Otherwise it will only ever work for the first call of your CheckButton() function instead of every call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the order of css selectors matter?

From Java

Why does order of mutable borrows matter in Rust?

From Dev

Why does declaration order matter for generic members?

From Dev

Pandas: Why does order of selection matter?

From Dev

Why does the order of template argument substitution matter?

From Dev

Why does order in method declaration matter?

From Dev

Why does this library dlopen order matter?

From Dev

why does the order of variable declaring matter?

From Dev

why does order of loop nesting matter python?

From Dev

Why does declaration order matter for generic members?

From Dev

Why does the order of alternatives matter in regex?

From Dev

Why does the order of applying advice matter?

From Dev

Why does order in method declaration matter?

From Dev

why does the order of variable declaring matter?

From Dev

why does order of loop nesting matter python?

From Dev

Why does the order of prerequisites matter in a makefile?

From Dev

Does the order of declaring functions and methods in C++ matter

From Java

Does annotations order matter?

From Dev

Does order of conditions in $and matter?

From Dev

Why one child class alone flings error ? does order matter?

From Dev

why does order matter in this escaped characters class in sed?

From Dev

Why does order matter in this usage of Observable.merge?

From Dev

Why does order matter when using "data" and "formula" keyword arguments?

From Dev

Why does order of `Object.include` and `Fixnum.prepend` matter?

From Dev

Why does the order of asynchronous and gen.coroutine matter in Tornado?

From Dev

Why does the order of the before_actions declaration matter?

From Dev

Why does order of comparison matter for this apply/lambda inequality?

From Dev

Why does the order of LET statements matter in this Entity Framework query?

From Dev

Spring Security java config - why does order of options matter?

Related Related

  1. 1

    Why does the order of css selectors matter?

  2. 2

    Why does order of mutable borrows matter in Rust?

  3. 3

    Why does declaration order matter for generic members?

  4. 4

    Pandas: Why does order of selection matter?

  5. 5

    Why does the order of template argument substitution matter?

  6. 6

    Why does order in method declaration matter?

  7. 7

    Why does this library dlopen order matter?

  8. 8

    why does the order of variable declaring matter?

  9. 9

    why does order of loop nesting matter python?

  10. 10

    Why does declaration order matter for generic members?

  11. 11

    Why does the order of alternatives matter in regex?

  12. 12

    Why does the order of applying advice matter?

  13. 13

    Why does order in method declaration matter?

  14. 14

    why does the order of variable declaring matter?

  15. 15

    why does order of loop nesting matter python?

  16. 16

    Why does the order of prerequisites matter in a makefile?

  17. 17

    Does the order of declaring functions and methods in C++ matter

  18. 18

    Does annotations order matter?

  19. 19

    Does order of conditions in $and matter?

  20. 20

    Why one child class alone flings error ? does order matter?

  21. 21

    why does order matter in this escaped characters class in sed?

  22. 22

    Why does order matter in this usage of Observable.merge?

  23. 23

    Why does order matter when using "data" and "formula" keyword arguments?

  24. 24

    Why does order of `Object.include` and `Fixnum.prepend` matter?

  25. 25

    Why does the order of asynchronous and gen.coroutine matter in Tornado?

  26. 26

    Why does the order of the before_actions declaration matter?

  27. 27

    Why does order of comparison matter for this apply/lambda inequality?

  28. 28

    Why does the order of LET statements matter in this Entity Framework query?

  29. 29

    Spring Security java config - why does order of options matter?

HotTag

Archive