   [Java] how do you "jump"?
  Hi all, I am very new to Java game development and am starting small to learn the basics before moving on.  With a little help from some tutorials and the java website I have created this "game" that lets you move up down left and right in a world.  I have "gravity" so when you go up you fall down to the ground.  My question now is this:  currently when I press the up arrow key, I can hold it down and my guy will keep floating up forever (until he hits the top boundary), I want it so when you press the up arrow key, it will only move up a certain distance every single time, instead of being able to float up as high as you want.Here is my current code:package Game;import java.awt.*;import java.awt.event.KeyEvent;import javax.swing.ImageIcon;public class Dude {   int x, dx, y, dy;   Image still, box1;   public Dude() {  ImageIcon i = new ImageIcon(getClass().getResource("dude.png"));  still = i.getImage();  x = 1000;  y = 147;   }   public void move() {  x += dx;  y += dy;  checkBoundaries();   }   public int getX() {  return x;   }   public int getY() {  return y;   }   public Image getImage() {  return still;   }   public Image getImage2() {  return box1;   }   private void checkBoundaries() {  if(x < 0) x = 0;  if(x > 1110) x = 1110;  if(y < 0) y = 0;  if(y > 147) y = 147;   }   public void keyPressed(KeyEvent e) {  int key = e.getKeyCode();  if (key == KeyEvent.VK_LEFT) dx = -1;  if (key == KeyEvent.VK_RIGHT) dx = 1;if (key == KeyEvent.VK_UP) dy = -5;    if (key == KeyEvent.VK_DOWN) dy = 1;   }   public void keyReleased(KeyEvent e) {  int key = e.getKeyCode();  if (key == KeyEvent.VK_LEFT) dx = 0;  if (key == KeyEvent.VK_RIGHT) dx = 0;  if (key == KeyEvent.VK_UP) dy = 1;  if (key == KeyEvent.VK_DOWN) dy = 1;   }}The: if (key == KeyEvent.VK_UP) dy = -5;is so when you hit the up arrow key he will "jump" at a faster rate than normal moving, I'm pretty sure I have to modify this section somehow   
  just keep track of what keys were pressed the previous frame.So, if up is pressed and it wasn't pressed last frame, then we jump.if up is pressed but last frame it was also pressed, then you don't jump because you have just jumped and are still in the air.if (key == KeyEvent.VK_UP && oldkey != KeyEvent.VK_UP)   dy = -5;//oldkey = key;   ;  So how do you get oldkey?  is it int oldkey = e.getKeyCode(); ?it makes sense to me but I don't really get how you get the oldkey.   ;  oldkey would be a datamember of your dude class to begin with.and the end of keypressed, you simply assign key to oldkey (oldkey = key).another possible fix for this could be working with booleans.bool isJumping; //assign to false in constructorif (key == keyevent.vk_up && !isJumping) {dy = -5; jumping=true;}// set jumping back to false at the moment your dude hits the groud;  Hmm I tried the boolean and that didn't work either :(I did:package Game;import java.awt.*;import java.awt.event.KeyEvent;import javax.swing.ImageIcon;public class Dude {   int x, dx, y, dy;   Image still, box1;   boolean isJumping;   public Dude() {  ImageIcon i = new ImageIcon(getClass().getResource("dude.png"));  still = i.getImage();  x = 1000;  y = 147;  isJumping = false;   }   public void move() {  x += dx;  y += dy;  checkBoundaries();   }   public int getX() {  return x;   }   public int getY() {  return y;   }   public Image getImage() {  return still;   }   public Image getImage2() {  return box1;   }   private void checkBoundaries() {  if(x < 0) x = 0;  if(x > 1110) x = 1110;  if(y < 0) y = 0;  if(y > 147) y = 147;   }   public void keyPressed(KeyEvent e) {  int key = e.getKeyCode();  if (key == KeyEvent.VK_LEFT) dx = -1;  if (key == KeyEvent.VK_RIGHT) dx = 1;   if (key == KeyEvent.VK_UP && !isJumping) {dy = -5; isJumping = true;}  isJumping = false;    if (key == KeyEvent.VK_DOWN) dy = 1;   }   public void keyReleased(KeyEvent e) {  int key = e.getKeyCode();  if (key == KeyEvent.VK_LEFT) dx = 0;  if (key == KeyEvent.VK_RIGHT) dx = 0;  if (key == KeyEvent.VK_UP) dy = 1;  if (key == KeyEvent.VK_DOWN) dy = 1;   }}Did I put the isJumping = false in the wrong place?   ;Quote:Original post by firecat318Hmm I tried the boolean and that didn't work either :(I did:*** Source Snippet Removed ***Did I put the isJumping = false in the wrong place?  if (key == KeyEvent.VK_UP && !isJumping) {dy = -5; isJumping = true;}  isJumping = false;This piece essentially sets isJumping to true and immediately back to false for one.You probably want isJumping = false in the keyreleased methods or whenever he hits the ground again.   