I have been trying to figure out how to make a visible image move diagonally in an applet window.
If you press up, down, left, or right the image (a gif) moves accordingly, however if you try to press two keys at once (up and right at the same time for example) the image only moves in the direction you pressed second (even if you press the keys at the same time there is still a microscopic delay).
There might be a simple way to fix this that I am just not aware of, or perhaps a workaround someone has figured out... I appreciate any help or advice that can be given.
Thank-you
Hero Class (this class defines what the "Hero" is; in this case a simple pixel man, and what he can do)
import objectdraw.*;
import java.awt.*;
public class Hero extends ActiveObject {
private DrawingCanvas canvas;
private VisibleImage player;
public Hero(Location initLocation, Image playerPic, DrawingCanvas aCanvas) {
canvas = aCanvas;
player = new VisibleImage(playerPic, canvas.getWidth()/3,
canvas.getWidth()/3, canvas);
start();
}
public void run()
{
}
public void move(double dx, double dy)
{
player.move(dx, dy);
}
}
HeroGame class (this class creates the "Hero" and specifies the location, as well as what keys are used to make him move)
import objectdraw.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HeroGame extends WindowController implements KeyListener {
private Hero theHero;
private Image playerPic;
private Location initLocation;
public void begin() {
playerPic = getImage("player.gif");
canvas.addKeyListener ( this );
this.addKeyListener ( this );
requestFocusInWindow();
theHero = new Hero(initLocation, playerPic, canvas);
}
public void keyTyped( KeyEvent e ) { }
public void keyReleased( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) {
if ( e.getKeyCode() == KeyEvent.VK_UP ) {
theHero.move(0,-5);
}
else if ( e.getKeyCode() == KeyEvent.VK_DOWN ) {
theHero.move(0,5);
}
else if ( e.getKeyCode() == KeyEvent.VK_LEFT ) {
theHero.move(-5,0);
}
else if ( e.getKeyCode() == KeyEvent.VK_RIGHT ) {
theHero.move(5,0);
}
}
}
Thank-you once more for taking the time to read this and hopefully help.