Search code examples
c#visual-studioxna

XNA - trying to use an IF to detect 2 gamepad presses at same time with no avail


Basically I have a task at college where I have to display the button presses of a gamepad from an xbox controller on the screen when they are pressed.

I have images to represent the gamepad presses and all the coordinates set out. I can get the buttons to appear when they are pressed... however. I must have the diagonal presses on the dPad to appear, without the corresponding buttons you need to press "NOT" apearing. I also need to have the triggers fade in with transparency as they are slowly pushed into their maximum value.

like this : Image of how the program should look when keys are pressed

I have the problem that when I press up and right together... all three images light up on screen (instead of just the diagonal up/right image).

I have basically set out IF statements within my drawing method to draw the specific images when they are pressed and to colour them each with a different colour, like this:

spriteBatch.Begin();
        spriteBatch.Draw(under, underPos, Color.White);
        if (GamePad.GetState(PlayerIndex.One).Triggers.Right == 1.0f)
        {
            spriteBatch.Draw(rightTrigger, rTriggerPos, Color.White);
        }
        if (GamePad.GetState(PlayerIndex.One).Triggers.Left == 1.0f)
        {
            spriteBatch.Draw(leftTrigger, lTriggerPos, Color.White);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
        {
            spriteBatch.Draw(right, rightPos, Color.Red);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
        {
            spriteBatch.Draw(left, leftPos, Color.Blue);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
        {
            spriteBatch.Draw(up, upPos, Color.Yellow);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
        {
            spriteBatch.Draw(down, downPos, Color.Green);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
        if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
        {
            spriteBatch.Draw(upRight, upRightPos, Color.Orange);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
        if (GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed)
        {
            spriteBatch.Draw(downRight, dRightPos, Color.Brown);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
        if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
        {
            spriteBatch.Draw(upLeft, upLeftPos, Color.Cyan);
        }
        if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
        if (GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed)
        {
            spriteBatch.Draw(downLeft, dLeftPos, Color.Purple);
        }
        spriteBatch.End();

My problem is I don't know how to combine the two IF statements to just show the one diagonal image, can you combine two IF statements or game presses at the sametime?... and how do you get the triggers to slowly increase in colour and transparency... can anyone help me with this?


Solution

  • You can have multiple conditions passed in an If statement using && for AND and || for OR.

    I would use else if instead of multiple if simply for clarity in this instance. Just a sample snippet from your code exercising this:

    else if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed && GamePad.GetState(PlayerIndex.One).DPad.Left == ButtonState.Pressed) { spriteBatch.Draw(upLeft, upLeftPos, Color.Cyan); }

    This will make the condition pass ONLY if both UP and LEFT are pressed at the same time.

    Hope this puts you in the right direction.