<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'keyboard input problem' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'keyboard input problem' posted on the 'Java' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sat, 18 May 2013 20:51:26 -0700</pubDate>
    <lastBuildDate>Sat, 18 May 2013 20:51:26 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>keyboard input problem</title>
      <link>http://www.programmersheaven.com/mb/java/417552/417552/keyboard-input-problem/</link>
      <description>I'm programming a little Mario game.&lt;br /&gt;
Now when the player is pressing left, the character is running left.&lt;br /&gt;
Now lets say the players press's up to jump. That immediately disables the left button that the player is still holding. Which is the problem.&lt;br /&gt;
Here is how I programed the keys&lt;br /&gt;
&lt;br /&gt;
addKeyListener(new KeyListener() {&lt;br /&gt;
		&lt;br /&gt;
	public void keyPressed(KeyEvent arg0) {&lt;br /&gt;
		int key=arg0.getKeyCode();&lt;br /&gt;
		if(key==KeyEvent.VK_LEFT){&lt;br /&gt;
			mario.moveLeft();&lt;br /&gt;
		}else if(key==KeyEvent.VK_RIGHT){&lt;br /&gt;
			mario.moveRight();&lt;br /&gt;
		}else if(key==KeyEvent.VK_UP){&lt;br /&gt;
			mario.startJumpSequence();&lt;br /&gt;
		}else if(key==KeyEvent.VK_END){&lt;br /&gt;
			running=false;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
So how can I make Mario move left when the player presses left, but when the player presses up (still holding left) mario jumps and moves left?&lt;br /&gt;
&lt;br /&gt;
Just to recap, I'm pressing left for Mario to move left. Now i want to jump as im moving left; as soon as I hit the up key, mario jumps but "forgets" to move left even though i never release "left".&lt;br /&gt;
How to fix this?&lt;br /&gt;
&lt;br /&gt;
Thank you for your time and help in advance&lt;br /&gt;
jenia&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/417552/417552/keyboard-input-problem/</guid>
      <pubDate>Tue, 29 Jun 2010 14:50:26 -0700</pubDate>
      <category>Java</category>
    </item>
    <item>
      <title>Re: keyboard input problem</title>
      <link>http://www.programmersheaven.com/mb/java/417552/417554/re-keyboard-input-problem/#417554</link>
      <description>You need to save the state of the movement.&lt;br /&gt;
&lt;br /&gt;
For example, when the left key is pressed, then do something like "movingLeft = true". When the left key is released (you should override keyReleased in the KeyListener), then do "movingLeft = false".&lt;br /&gt;
&lt;br /&gt;
If you do this for every key, then you know exactly which keys are pressed at the moment. Then, every time you update Mario's position, you can check these variables, so your periodically updated game loop would look something like this:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
if(movingLeft){
  mario.moveLeft();
}
if(movingRight){
  mario.moveRight();
}
&lt;/pre&gt;&lt;br /&gt;
Well, that's the general idea. I hope it helps!</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/417552/417554/re-keyboard-input-problem/#417554</guid>
      <pubDate>Tue, 29 Jun 2010 15:27:00 -0700</pubDate>
      <category>Java</category>
    </item>
  </channel>
</rss>