jeudi 15 septembre 2016

XNA - Update only runs one if statement

I'm working on a game in XNA and have some trouble with the Update() method. I've made a class called mouseHandler which can check if the mouse hovers over a rectangle or if there's a leftclick. Now, it's the leftclick method that doesn't work with my Update(). Here's the leftclick method:

        public bool leftClick(Rectangle rect)
    {
        oldMouse = mouse;
        mouse = Mouse.GetState();   // single click
        mousePos = new Point(mouse.X, mouse.Y);

        return rect.Contains(mousePos)    // check if mouse is inside the rectangle
            && mouse.LeftButton == ButtonState.Pressed 
            && oldMouse.LeftButton == ButtonState.Released;
    }

I know it might not be the most optimal way to check for a leftclick... Anyways, this method is called in my Game class. I want to check if either two of my sprites (rectangles that belong to them) are clicked on. This gives two if statements. To make it clear I've only made a Console.WriteLine() in these methods. The problem is that it only checks for the first one. Code:

                if (mouse.leftClick(girlRect))   // girl rectangle
                {
                    Console.WriteLine("girl");
                }

                if (mouse.leftClick(boyRect))    // boy rectangle
                {
                    Console.WriteLine("boy");
                }

By executing my game the console only writes when I click the girl; nothing happens if I click the boy. Can anyone tell me how to fix this or what I'm doing wrong?

By the way: If I use my hover() method on the girl and boy instead of leftClick() it works fine. I should also mention that the if statements are inside a switch-case but other than that the Update() is empty.

Aucun commentaire:

Enregistrer un commentaire