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.
I would suggest you use Swing (JApplet) and Key Bindings. See Motion Using the Keyboard for problems with using a KeyListener along with a solution for handling multiple keys being pressed at the same time.
change
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);
}
to
if ( e.getKeyCode() == KeyEvent.VK_UP ) {
theHero.move(0,-5);
}
if ( e.getKeyCode() == KeyEvent.VK_DOWN ) {
theHero.move(0,5);
}
if ( e.getKeyCode() == KeyEvent.VK_LEFT ) {
theHero.move(-5,0);
}
if ( e.getKeyCode() == KeyEvent.VK_RIGHT ) {
theHero.move(5,0);
}
And it should react on both keys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With