0

I am creating a doodle jump game and I am very confused. I have to make the doodle jump but I am so lost. I have put the keys (space, A, D) into the KeyListener and I am not sure why it's not working. It is registering that I pressed space, but I am not sure how to make the Doodle actually jump. I tried doing the translate method but that does not work.

    import java.awt.Color;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;


    public class DoodleJumpFrame extends JFrame implements KeyListener
    {
     private static final int FRAME_WIDTH = 600;
     private static final int FRAME_HEIGHT = 500;

     private JPanel panel;
     private DoodleJumpComponent game;


public DoodleJumpFrame() 
{
    super();
    panel = new JPanel();
    game = new DoodleJumpComponent();
    init();
}

public void init()
{
    panel.add(game);
    panel.setBackground(Color.WHITE);

    addKeyListener(this); 
}

public void addPanel() 
{
    this.add(panel);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

public void run() 
{
    game.start();
}

@Override
public void keyPressed(KeyEvent e)
{
   
   int keycode = e.getKeyCode(); 
   if (keycode == KeyEvent.VK_SPACE)
   {
      game.setSpacePressed(true);
   
   }
   
   if (keycode == KeyEvent.VK_A)
   {
      game.setLeftPressed(true);
   
   }
   if (keycode == KeyEvent.VK_D)
   {
      game.setRightPressed(true);
   
   }
   
 
    
}

@Override
public void keyReleased(KeyEvent e)
{
   int keycode = e.getKeyCode(); 
   if (keycode == KeyEvent.VK_SPACE)
   {
      game.setSpacePressed(false);
      System.out.println("yes");
   }
   
   if (keycode == KeyEvent.VK_A)
   {
      game.setLeftPressed(false);
   
   }
   if (keycode == KeyEvent.VK_D)
   {
      game.setRightPressed(false);
   
   }
    
    
}

@Override
public void keyTyped(KeyEvent e)
{
    // TODO Auto-generated method stub
    
}

}

My other class:

       import java.awt.Image;
      import java.awt.Rectangle;
     import java.awt.geom.Rectangle2D;
     import java.util.ArrayList;

     import javax.swing.ImageIcon;


      public class Doodle
     {
       private ImageIcon io;
       private Image i;
       private float x, y = 0;
       private double speedX, speedY = 0;
       private static int PIC_HEIGHT = 45;
       private static int PIC_WIDTH = 45;
       private boolean leftPressed, rightPressed, spacePressed, UPressed, IPressed;
       private Rectangle2D hitbox = new Rectangle((int)x,(int)y,PIC_HEIGHT,PIC_WIDTH);
   
   public Rectangle2D getHitbox()
   {
       return hitbox;
   }
   public void setHitbox(Rectangle2D hitbox)
   {
       this.hitbox = hitbox;
   }
   public Doodle(int x, int y) 
   {
       this.x = x;
       this.y = y;
       io = new ImageIcon(this.getClass().getResource("doodle.jpeg"));
       i = io.getImage();
   }
   
   public void tick(ArrayList<Platform> tiles) 
   {
       hitbox.setRect((int)x,(int)y,PIC_HEIGHT,PIC_WIDTH);
       move(tiles);
   }
   public void move(ArrayList<Platform> tiles) {
       
       if(rightPressed) {
           x+=5;
       }
       if(leftPressed) {
           x-=5;
       }
       if(x>320-PIC_WIDTH) {
           speedX = 0;
           x = 320-PIC_WIDTH;
       }
       if(x<0) {
           speedX = 0;
           x = 0;
           
      
       }
       jump(tiles);
   }
   public void jump(ArrayList<Platform> tiles) {
       
       for(int i = 0; i<tiles.size(); i++) {   
           if(hitbox.intersects(tiles.get(i).getRect())||y+PIC_HEIGHT>620) {
               speedY=-15;
           }
       }
       if(y+PIC_HEIGHT<620) speedY+=1;
       speedY*=0.9;
       y+=speedY;
       
   }
   public ImageIcon getIo()
   {
       return io;
   }
   public void setIo(ImageIcon io)
   {
       this.io = io;
   }
   public Image getI()
   {
       return i;
   }
   public void setI(Image i)
   {
       this.i = i;
   }
   public float getX()
   {
       return x;
   }
   public void setX(float x)
   {
       this.x = x;
   }
   public float getY()
   {
       return y;
   }
   public void setY(float y)
   {
       this.y = y;
   }
   public static int getPIC_HEIGHT()
   {
       return PIC_HEIGHT;
   }
   public static void setPIC_HEIGHT(int pIC_HEIGHT)
   {
       PIC_HEIGHT = pIC_HEIGHT;
   }
   public static int getPIC_WIDTH()
   {
       return PIC_WIDTH;
   }
   public static void setPIC_WIDTH(int pIC_WIDTH)
   {
       PIC_WIDTH = pIC_WIDTH;
   }
   
   
   public void leftPressed(boolean leftPressed)
   {
       this.leftPressed =leftPressed;      
   }
   public void rightPressed(boolean rightPressed)
   {
       this.rightPressed = rightPressed;           
   }
   public void spacePressed(boolean spacePressed)
   {
       this.spacePressed = spacePressed;       
   }
   public void UPressed(boolean UPressed)
   {
       this.UPressed = UPressed;
   }
   public void IPressed(boolean IPressed)
   {
       this.IPressed = IPressed;
   }
 
   

}

maloomeister
  • 2,461
  • 1
  • 12
  • 21
Sarah
  • 11
  • 2
  • Is this all of your code? I'm not seeing your [main game loop](https://dewitters.com/dewitters-gameloop/). – Tim Hunter May 20 '21 at 16:16
  • So you're saying the key listener isn't working? It has nothing to do with your jump? – matt May 21 '21 at 10:24
  • https://stackoverflow.com/a/8961998/2067492 that has a lot of help. It provides concrete examples using a keylistener (not a great idea) and using key bindings (much better). – matt May 21 '21 at 10:28

0 Answers0