Event Handling problems

Started by
1 comment, last by 6510 11 years, 11 months ago
Hi, I am working on one of my first games in java and I was having a big problem with the Event Handler where it would only work somewhere in the realm of 1/10 times. If anyone has any idea on how to solve this problem I'd be much obliged.

This is my game panel constructor, I have global variables called isRight, isDown etc... and in the main gameloop I check if they are true
in which case my character moves so many pixels

public GamePanel(){
setFocusable(true);
requestFocus();
setPreferredSize(new Dimension(GWIDTH,GHEIGHT));
makeGrid();
setBackground(Color.white);
addKeyListener(new KeyListener(){

public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = true;
System.out.println("Rightpressed");
break;
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
default:
break;
}

}

public void keyReleased(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = false;
System.out.println("Rightreleased");
break;
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
default:
break;
}

}

public void keyTyped(KeyEvent e) {


}

});
}

This here is the update methods and run() game loop, I don't think that the problem is in here because I put in a print statement in each of these methods and they ran consistently.

public void gameUpdate()
{
//update game logic

if(isRight){
player.x += player.speed;
}
if(isLeft){
player.x -=player.speed;
}

}
public void run()
{
while(running)
{

gameUpdate();
gameRender();
paintScreen();
try{
game.sleep(20);
}
catch(Exception e){}
}
}


And Just incase it is not a problem with my constructor, here is my complete code if anyone is willing to look through it


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GamePanel extends JPanel implements Runnable{

/**
*
*/
private static final long serialVersionUID = 1L;
//double buffering
Image dbImage;
Graphics dbg;

Rectangle[] grid = new Rectangle[250];

//JPanel Vars
public static final int GWIDTH = 500, GHEIGHT = 500;
static final Dimension gameDim = new Dimension(GWIDTH,GHEIGHT);

public boolean isRight = false,isLeft = false, isUp = false,isDown=false;

private Thread game;
private volatile boolean running = false;

Player player = new Player();
public GamePanel(){
setFocusable(true);
requestFocus();
setPreferredSize(new Dimension(GWIDTH,GHEIGHT));
makeGrid();
setBackground(Color.white);
addKeyListener(new KeyListener(){

public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = true;
System.out.println("Rightpressed");
break;
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
default:
break;
}

}

public void keyReleased(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
isRight = false;
System.out.println("Rightreleased");
break;
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
default:
break;
}

}

public void keyTyped(KeyEvent e) {


}

});
addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

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

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

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

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

}

});

}
private void makeGrid(){
int x=0,y=0;
for(int i = 0; i <grid.length;i++)
{
if(i % 16 != 0 | i ==0){
grid = new Rectangle(x,y,32,32);
x+=32;
}
else{
y+=32;
x = 0;
grid = new Rectangle(x,y,32,32);
x+=32;
}
}
}
private void startGame()
{
if(game == null || !running){
running = true;
game = new Thread(this);
game.start();

}
}
public void stopGame()
{
if(game != null || running)
{
running = false;
}
}
public void addNotify()
{
super.addNotify();
startGame();
}
//Game Rendering
public void gameUpdate()
{
//update game logic

if(isRight){
player.x += player.speed;
}
if(isLeft){
player.x -=player.speed;
}

}
private void gameRender()
{
if(dbImage == null)
{
dbImage = createImage(GWIDTH,GHEIGHT);
if(dbImage == null)
{
System.err.println("Error in Game Render");
return;
}
else
{
dbg = dbImage.getGraphics();
}
}
dbg.setColor(Color.WHITE);
dbg.fillRect(0, 0, getWidth(),getHeight());
//drawGAME
draw(dbg);
}
public void draw(Graphics g)
{
//draw things here
g.setColor(Color.black);
//player.draw(g);
g.fillRect(player.x, player.y, player.width, player.height);

g.setColor(Color.RED);
for(int i = 0; i < grid.length; i++){
g.drawRect(grid.x, grid.y, grid.width, grid.height);
}
}

public void paintScreen()
{
Graphics g;
try{
g = this.getGraphics();
if(dbImage != null && g!= null){
g.drawImage(dbImage,0,0, null);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
catch(Exception e)
{
System.err.println("Error in paintScreen");
}

}
//logic
public void run()
{
while(running)
{

gameUpdate();
gameRender();
paintScreen();
try{
game.sleep(20);
}
catch(Exception e){}
}
}
}
Advertisement
Are you sure the panel has focus? you request focus in the constructor, but I doubt it will have any effect, since the panel is not added to any frame/applet before the entire constructor is executed any ways. Try to give it focus from the frame after you have added the panel
What exactly doesn't work ? Already set some break points and debugged ?
In any case, your direction variables should be volatile or synchronized.

This topic is closed to new replies.

Advertisement