Walking around world problem

Alright I'm trying to make a simple game, as it's my first game that not a simple puzzle game. Right now I'm attempting to make the player just walk around on screen. However for some reason it keeps allowing the player to move off screen. I can't figure out why. Any help would be aprreciated.

Driver:

import javax.swing.JFrame;

publicclass Direction

{

publicstaticvoid main(String[] args)

{

JFrame frame=new JFrame("Direction");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add (new DirectionPanel());

frame.pack();

frame.setVisible(true);

}

}

Program:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass DirectionPanelextends JPanel

{

privatefinalint WIDTH=600, HEIGHT=500;

privatefinalint JUMP=20;

privatefinalint IMAGE_SIZE=20;

private ImageIcon up, down, right, left, currentImage;

privateint x, y, moveX, moveY;

public DirectionPanel()

{

addKeyListener (new DirectionListener());

x=40;

y=40;

moveX=moveY=40;

up=new ImageIcon("walkingUp.gif");

down=new ImageIcon("walkingDown.gif");

left=new ImageIcon("walkingLeft.gif");

right=new ImageIcon("walkingRight.gif");

currentImage= right;

setBackground (Color.orange);

setPreferredSize (new Dimension(WIDTH, HEIGHT));

setFocusable(true);

}

publicvoid paintComponent(Graphics page)

{

super.paintComponent(page);

currentImage.paintIcon (this, page, x, y);

}

privateclass DirectionListenerimplements KeyListener

{

publicvoid keyPressed(KeyEvent event)

{

switch(event.getKeyCode())

{

case KeyEvent.VK_UP:

currentImage=up;

moveY-=JUMP;

break;

case KeyEvent.VK_DOWN:

currentImage=down;

moveY+=JUMP;

break;

case KeyEvent.VK_LEFT:

currentImage=left;

moveX-=JUMP;

break;

case KeyEvent.VK_RIGHT:

currentImage=right;

moveX+=JUMP;

break;

}

if(moveY!=20||moveY!=HEIGHT-20||moveX!=20||moveX!=WIDTH-20)

{

y=moveY;

x=moveX;

}

repaint();

}

publicvoid keyTyped(KeyEvent event){}

publicvoid keyReleased(KeyEvent event){}

}

}

[4984 byte] By [davegelha] at [2007-11-26 19:54:03]
# 1

> Alright I'm trying to make a simple game, as it's my

> first game that not a simple puzzle game. Right now

> I'm attempting to make the player just walk around on

> screen. However for some reason it keeps allowing the

> player to move off screen. I can't figure out why.

> Any help would be aprreciated.

Hi, although your figure (image) has already reached the end of the panel, you still increase (or decrease) it's position in the switch. Try to check in the switch statement before moving it. Something like this:

private class DirectionListener implements KeyListener

{

public void keyPressed(KeyEvent event)

{

switch(event.getKeyCode())

{

case KeyEvent.VK_UP:

currentImage=up;

y = y > 0 ? y-JUMP : y;

break;

case KeyEvent.VK_DOWN:

currentImage=down;

y = y < HEIGHT-JUMP ? y+JUMP : y;

break;

case KeyEvent.VK_LEFT:

currentImage=left;

x = x > 0 ? x-JUMP : x;

break;

case KeyEvent.VK_RIGHT:

currentImage=right;

x = x < WIDTH-JUMP ? x+JUMP : x;

break;

}

repaint();

}

public void keyTyped(KeyEvent event){}

public void keyReleased(KeyEvent event){}

}

Note: I did not test it, but you'll get the idea.

Good luck.

prometheuzza at 2007-7-9 22:46:23 > top of Java-index,Other Topics,Java Game Development...
# 2
Thanks for your help. That worked.
davegelha at 2007-7-9 22:46:23 > top of Java-index,Other Topics,Java Game Development...