Jump to content

  • Log In with Google      Sign In   
  • Create Account

joe96

Member Since 31 Aug 2011
Offline Last Active Apr 21 2013 12:57 AM

Topics I've Started

Problem with Java KeyListener

20 April 2013 - 11:49 AM

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.

Top - down 2d zombie game

02 September 2011 - 10:46 PM

I've been working on a top - down 2d zombie shooter. I've got all the player movement down but i have been having trouble with the shooting. I cant figure out how to make it shoot in the direction of where the mouse was clicked. I use java to program it. If anyone can help that would be great.

Randomly generated terrain

31 August 2011 - 05:54 PM

I'm making a 2d side scrolling game. I want a randomly generated world similar to Terraria. If you've never heard of it the website is here: http://www.terraria.org . I've tried many different things but I can't figure out how to. If anyone can help that would be great.

PARTNERS