I tried to figure out how to make a visible image diagonally in the applet window.
If you press up, down, left or right, the image (gif) moves accordingly, however, if you try to simultaneously press two keys (for example, up and right), the image moves only when you pressed the second one (even if you press the keys in the same time, there is still a microscopic delay).
Perhaps there is an easy way to fix this, which I simply donβt know about, or perhaps a workaround that someone has figured out ... I appreciate any help or advice that can be given.
Thanks
Hero Class (this class defines what a "Hero" is, in this case a simple person with pixels 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 a "Hero" and indicates the location, as well as the keys that are used to move it)
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); } } }
Thanks again for taking the time to read this and hopefully help.