-1
\$\begingroup\$

I am using the tk2d toolkit.

For my project, each player has their own button. I need to be able to get both buttons inputs (at the same time) so that I can figure out which up event first.

I run into the problem in this situation.

  • First User presses their button down.
  • Second user presses their button down, at the same time the first user is releasing their button.
  • Button inputs aren't registered properly

I need to be able to determine who's up event fired first, while still letting both players push their buttons whenever they want.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ He's using unikronsoftware.com/2dtoolkit with Unity3D. However, this is just barely gamedev question, but it was easy answer with complete examples for unity. I don't think it's unclear, but can't vote for it. \$\endgroup\$
    – Katu
    Jul 1, 2014 at 15:59
  • \$\begingroup\$ You have to give more information after reading only that much detail I can't able to find out your problem. I have two concept in mind. First you want button down effect for both buttons. Second either pressing any button you want to perform both things. Which one you are asking that you have to say?? \$\endgroup\$
    – Siddharth
    Jul 2, 2014 at 4:47
  • \$\begingroup\$ Yea Siddharth Bhai, I have 2 buttons for 2 user means each user have its own button. User have two events button down and button up.I want to Find Which user's button up event fired first. I used tk2d for perform those things. But main problem is that When 1st user press own button down then after 2nd user press its own button down at that same time 1st users button automatically button up so how can i solve this problem.Means that both user cant press button down in this situation. I want both user can fired its own button down and up both event whatever he/she want. Thanking To You. \$\endgroup\$ Jul 2, 2014 at 5:33
  • \$\begingroup\$ Even after the edit, this confuses me. What do you mean by "at the same time"? What have you tried and why was the result different from what you want? \$\endgroup\$
    – Anko
    Jul 2, 2014 at 7:08
  • \$\begingroup\$ Anko : Means that first i pressed and "hold down" first button then i pressed and "hold down" second button at the same time first button automatically fire button up event even i m hold down. That is main problem. \$\endgroup\$ Jul 2, 2014 at 9:14

2 Answers 2

3
\$\begingroup\$

Use the AND symbol to test if both buttons are down in an if statement. In C++ and C# it would look something like this:

if(BUTTON_A_PRESSED == true && BUTTON_B_PRESSED == true) { /* DO SOMETHING */ }

Otherwise you could use a nested if statement like so:

if(BUTTON_A_PRESSED == true)
{ if(BUTTON_B_PRESSED == true) { /* DO SOMETHING */ }  }

Hope this helps.

EDIT:

After reading the edit this may help.

If you want both buttons to be pressed down at the same time, but cause separate events to go off per user you will need to add the button down or button up event separately. If they are separate they will trigger individually, regardless of what the other button is doing. If you run this frame by frame you can add in a bool (or an int for multiple buttons) to to see who pressed first.

Here I use int to easier show the player number that pressed first:

int first = 0;

if (BUTTON_A_DOWN == true)
{
   if (first == 0)
   {
      first = 1;
      /* DO SOMETHING */
   }
}

if (BUTTON_B_DOWN == true)
{
   if (first == 0)
   {
      first = 2;
      /* DO SOMETHING */
   }
}

You can then use the int or bool to see who pressed first. You will need to decide how often you want to run this, as if it is every frame it will have seemingly no effect.

However, should you just want both players to react individually, using the int or bool is not needed as they will just check for the button press/release in order. This happens so fast that it won't be noticed by the players.

\$\endgroup\$
1
\$\begingroup\$

It's easy to check if those are both down. Just use IF with && and operator. Here is two examples for Unity, ready to run. Just place these to any script on an object that is in scene.

function Update () {
    if (Input.GetKey ("up") && Input.GetKey ("down"))
        print ("Pressing Up and Down");
}

However, this activates every frame and it can cause weird stuff, if you only need to react once to that double key press. This is how you can check double key press and react only once to it. Once you release either of keys, it can be activated again.

bool keysDown = false;
// Update is called once per frame
void Update()
{
    if (keysDown == false && Input.GetKey("up") && Input.GetKey("down"))
    {
        keysDown = true;
        print("Pressed keys down");
    }
    else if (keysDown == true && ( Input.GetKey("up") == false || Input.GetKey("down") == false))
    {
        keysDown = false;
        print("Released one of the keys");
    }
}
\$\endgroup\$

You must log in to answer this question.

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