Note that there are some explanatory texts on larger screens.

plurals
  1. POPlayer not moving in 2D Game
    text
    copied!<p>I can not seem to get my player to move when certain keys are pressed. I did some debugging and placed a <code>System.out.print("Pressed);</code> in the if statements that check if a key is pressed and it prints out the message every time I press the key, they player just is not moving. Can someone help!</p> <p>here is the InputHandler class</p> <pre><code>public class InputHandler implements KeyListener { public InputHandler(Game game) { game.addKeyListener(this); } public class Key { private boolean pressed = false; public void toggle(boolean isPressed) { pressed = isPressed; } public boolean isPressed() { return pressed; } } // public List&lt;Key&gt; keys = new ArrayList&lt;Key&gt;(); public Key up = new Key(); public Key down = new Key(); public Key left = new Key(); public Key right = new Key(); public void keyPressed(KeyEvent e) { toggleKey(e.getKeyCode(), true); } public void keyReleased(KeyEvent e) { toggleKey(e.getKeyCode(), false); } public void keyTyped(KeyEvent e) { } public void toggleKey(int keyCode, boolean isPressed) { if (keyCode == KeyEvent.VK_W) { up.toggle(isPressed); } else if (keyCode == KeyEvent.VK_S) { down.toggle(isPressed); } else if (keyCode == KeyEvent.VK_A) { left.toggle(isPressed); } else if (keyCode == KeyEvent.VK_D) { right.toggle(isPressed); } } } </code></pre> <p>and here is the Player class</p> <pre><code>public class Player extends Mob { private InputHandler input; private int r = 10; public Player(int x, int y, int speed, InputHandler input) { super("Player", x, y, 1); this.input = input; this.x = x; this.y = y; } public boolean hasCollided(int dx, int dy) { return false; } public void update() { int dx = 0; int dy = 0; if (input.up.isPressed()) { dy--; } else if (input.down.isPressed()) { dy++; } else if (input.left.isPressed()) { dx--; } else if (input.right.isPressed()) { dx++; } if (dx != 0 || dy != 0) { move(dx, dy); isMoving = true; } else { isMoving = false; } if (x &lt; r) x = r; if (y &lt; r) y = r; if (x &gt; Game.WIDTH - r) x = Game.WIDTH - r; if (y &gt; Game.HEIGHT - r) y = Game.HEIGHT - r; } public void render(Graphics2D g) { g.setColor(Color.BLACK); g.fillOval(x - r, y - r, 2 * r, 2 * r); g.setStroke(new BasicStroke(3)); g.setColor(Color.GRAY); g.drawOval(x - r, y - r, 2 * r, 2 * r); g.setStroke(new BasicStroke(1)); } } </code></pre> <p>and here is the Mob class that has the <code>move()</code> method in it</p> <pre><code> public Mob(String name, int x, int y, int speed) { this.name = name; this.speed = speed; } public void move(int dx, int dy) { if (dx != 0 &amp;&amp; dy != 0) { move(dx, 0); move(dy, 0); numSteps--; return; } numSteps++; if (!hasCollided(dx, dy)) { if (dy &lt; 0) movingDir = 0; if (dy &gt; 0) movingDir = 1; if (dx &lt; 0) movingDir = 2; if (dx &gt; 0) movingDir = 3; x += dx * speed; y += dy * speed; } } public abstract boolean hasCollided(int dx, int dy); public String getName() { return _name; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload