Need help with pressing multiple keys in java

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3565 Views - Last Post: 14 November 2010 - 07:05 AM Rate Topic: -----

#1 h3nchCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Need help with pressing multiple keys in java

Posted 07 November 2010 - 10:15 AM

Hi guys,

I'm trying to code a game similar to Mario in Java and am having problems with making the character jump while he is running. What the below code ends up doing is, as the user is holding down right and presses the jump button, Mario basically stops running and jumps, so its like the user has let go of right and pressed jump.

public void keyPressed(KeyEvent e) {
// here I keep track of what buttons the user pressed
    int keyCode = e.getKeyCode();

    if(keyCode == 37)
        pressedKeys[0] = true;
    else if(keyCode == 39)
        pressedKeys[1] = true;
    else if(keyCode == 68)
        pressedKeys[2] = true;

    // after I see what the user has pressed an action is carried out
    Thread t = new Thread(this);
    t.start();
}

public void performAction()
{
    // depending on what the user has pressed a certain action is performed
    if(pressedKeys[2]==true)
    {
        // changes the coordinates of the character itself
        // done in another thread so the background can continue
        // moving as the user holds down a direction
        Thread t = new Thread(mcControl);
        t.start();
    }
    if(pressedKeys[0]==true)
    {
        changeSprite();
        bg.moveImageForward();
    }
    if(pressedKeys[1]==true)
    {
        changeSprite();
        bg.moveImageBackward();
    }
}

public void run() {
    performAction();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with pressing multiple keys in java

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Need help with pressing multiple keys in java

Posted 07 November 2010 - 04:48 PM

I think you'll need to make separate KeyListeners for the panel so that one controls running and the other controls jumping
Was This Post Helpful? 1
  • +
  • -

#3 h3nchCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Need help with pressing multiple keys in java

Posted 07 November 2010 - 04:57 PM

View PostLuckless, on 07 November 2010 - 03:48 PM, said:

I think you'll need to make separate KeyListeners for the panel so that one controls running and the other controls jumping



Ahhh, thanks man, something so simple yet effective. I'll try this out tomorrow as its late atm.
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Need help with pressing multiple keys in java

Posted 07 November 2010 - 05:00 PM

sounds good. Let us know how it goes
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: Need help with pressing multiple keys in java

Posted 07 November 2010 - 06:22 PM

View PostLuckless, on 07 November 2010 - 07:48 PM, said:

I think you'll need to make separate KeyListeners for the panel so that one controls running and the other controls jumping

Since the KeyEvents are all dealt with by the EventDispatchThread, this probably won't make much of a difference.

Instead, let's break it down into states. The states we care about are:
-Max height
-Jump button pressed/released
-Right/left buttons being pressed/released

So, when one left/right direction button is pressed, the velocity should move in that direction. When the button is released, the velocity becomes 0. The jumping works basically the same way, with the exception of max height, as you don't want Mario jumping over everyone. These states should be separate and parameterized, not related beyond painting based on the coordinates.

You may also want to check out my tutorial on 2D Gravity.
Was This Post Helpful? 1
  • +
  • -

#6 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Need help with pressing multiple keys in java

Posted 07 November 2010 - 06:28 PM

I also noticed that you use else if statements. If you use all if statements, then ALL the keys that are pressed will signal that they are pressed, not just one of them.
Was This Post Helpful? 1
  • +
  • -

#7 h3nchCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Need help with pressing multiple keys in java

Posted 08 November 2010 - 01:49 PM

Ok, I tried multiple listeners out but as macosxnerd said because they are dealt with by the same thread both still fire the keypressed event for d, stopping mario in his tracks

The thing is I'm pretty much useless at maths (as I haven't had lessons on it for ages) so the tutorial posted, while informative, does nothing but confuse me as it expects the reader to have some experience with maths
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Need help with pressing multiple keys in java

Posted 08 November 2010 - 02:39 PM

Like I said....you should use multiple ifs. This is essentially what you have...

if (key_up) 
    // Do something
else if (key_right)
    // ...
else if (key_left)
    // ...



If key_up is true, then NONE of the other events will matter and are ignored, which simply makes Mario jump with no forward motion. You should use multiple ifs instead:

 
if (key_up) 
    // Do something
if (key_right)
    // ...
if (key_left)
    // ...



That way, if multiple buttons are pressed, it does multiple things. A suggestion that I have that you may be interested in using is the idea of an input manager class, which keeps track of currently pressed keys without worrying about the "Windows-delayed-repeating syndrome" (Where the first time you hold it, it delays.

Here's the tutorial:
http://www.dreaminco...-manager-class/
Was This Post Helpful? 1
  • +
  • -

#9 h3nchCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Need help with pressing multiple keys in java

Posted 08 November 2010 - 05:14 PM

I have changed my code to use an input manager but the main problem that I'm having is that if I'm holding down the right key and press (and release) the 'd' key, the program doesn't realise that I am still holding down the right key (so the keyPressed method is not activated meaning it wont move the character in any direction).
Was This Post Helpful? 0
  • +
  • -

#10 h3nchCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Need help with pressing multiple keys in java

Posted 08 November 2010 - 05:40 PM

View Posth3nchCoder, on 08 November 2010 - 04:14 PM, said:

I have changed my code to use an input manager but the main problem that I'm having is that if I'm holding down the right key and press (and release) the 'd' key, the program doesn't realise that I am still holding down the right key (so the keyPressed method is not activated meaning it wont move the character in any direction).


By the way I would also appreciate if someone told me whether or not this is possible without threads because once I start using them my code becomes a tangled mess and usually gets way too complicated
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: Need help with pressing multiple keys in java

Posted 08 November 2010 - 05:53 PM

Yes- in fact, I would highly recommend not using Threads. Try using Swing Timer instead to invoke repaint(). Then on each invocation of paint(), update the character's parameters (x, y) and paint it back on the screen. There are a bunch of tutorials on this in the Java Tutorials Section.

Basically, the change in x takes on one of two values: 0 or some change value. Now with the y value, you can use the kinematics equation y = velInitial*t + .5at2, with t based on the intervals between repaint() invocations.
Was This Post Helpful? 1
  • +
  • -

#12 h3nchCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Need help with pressing multiple keys in java

Posted 09 November 2010 - 01:54 PM

Thank you everyone who contributed to this thread, you people are truly awesome, I've finally got it working on a simple example (basically its just moving a square around with key presses), I could post this up if anyone would like to have a look at it.

:D
Was This Post Helpful? 0
  • +
  • -

#13 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: Need help with pressing multiple keys in java

Posted 09 November 2010 - 02:18 PM

yeah please post it up, would be a nice reference :)
Was This Post Helpful? 0
  • +
  • -

#14 h3nchCoder  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 07-November 10

Re: Need help with pressing multiple keys in java

Posted 10 November 2010 - 01:00 PM

Before I upload this file I must say a few things. What I have done is not well commented but should be self explanatory as its a simple example (also most of the concepts used are discussed within this thread). The code will not show you how to make mario jump as he is running (it is strictly an example of how to press multiple keys at once). Finally, this file may not stay up for long so if it goes down send me a pm and I'll hopefully re-upload it.

http://www.megaupload.com/?d=PEPKE1L5
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,244
  • Joined: 27-December 08

Re: Need help with pressing multiple keys in java

Posted 10 November 2010 - 05:18 PM

Could you instead post the code into the body of your post? :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2