How to move the visible image diagonally?

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.

+6
source share
5 answers

This is a fairly common situation in games that accept keyboard input. The problem is that your game must act on all the keys that are currently pressed before the game (this means that all keys for which the KEY_RELEASE event has not yet been triggered).

KeyListener will only notify you of the last key pressed (and will continue to notify you of the same key if you hold it pressed), so you need to track the status of the remaining other keys yourself.

To get an idea of ​​how to do this, search for a keyboard survey ( here is an example ) - a popular method in which you do not act immediately on the input of the player, but rather keep keystrokes in the queue, and then poll this queue at regular intervals (i.e. in your game loop) to decide which keys are pressed or not, and act accordingly.

I hope this helps

0
source

I would suggest using Swing (JApplet) and Key Bindings. See Keyboard Movement for problems using KeyListener along with a solution for pressing multiple keys simultaneously.

+3
source

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 he must respond to both keys.

+2
source

Instead of your if-else for the key, you should use a series of if .

Instead of setting the hero direction for x and y movement in these if you should only update delta

For a (very simplified) example ...

 if (up) yDelta = -5; if (down) yDelta = 5; if (left) xDelta = -5; if (right) xDelta = 5; hero.move(xDelta, yDelta); 

I also agree with camickr

+1
source

What you need to do is access the base class of the AffineTransform class and set the correct translation methods. This will allow you to move the image anyway (by any number of degrees in any direction) with the click of a key (which you associate with this call).

Read about it: http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html

0
source

Source: https://habr.com/ru/post/951250/


All Articles