Jump to content
  • Advertisement

joe96

Member
  • Content Count

    6
  • Joined

  • Last visited

Everything posted by joe96

  1. I'm working on a 2d game in java. I have a problem using the key listener to move the character. I might be typing the code wrong or something but i need help fixing it. This is my player.java class:   import java.awt.event.*;   public class Player implements KeyListener{   public int x = 500, y = 300;  public int health = 100; public int dx; public int dy;   public Player(){ x = x + dx; y = y + dy; }   public int getXPos(){ return x; }   public int getYPos(){ return y; }   public int getHealth(){ return health; }     public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_W){ dy = 2; } if(keyCode == KeyEvent.VK_A){ dx = -2; } if(keyCode == KeyEvent.VK_S){ dy = -2; } if(keyCode == KeyEvent.VK_D){ dx = 2; } }   public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_W){ dy = 0; } if(keyCode == KeyEvent.VK_A){ dx = 0; } if(keyCode == KeyEvent.VK_S){ dy = 0; } if(keyCode == KeyEvent.VK_D){ dx = 0; } }     }     In a different class i draw the character as a rectangle like this:   public void paint(Graphics g){ super.paint(g); g.setColor(Color.BLACK); g.fillRect(p.getXPos(), p.getYPos(), 25, 25); }     The problem is the character doesn't move when i press the buttons. I'm relatively new to java. Any help will be greatly appreciated.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!