0

So im a newbie and im trying to get my game to get input from keyboard to move a fixed distance (lets say 50) so if you press up on arrow key you go up by 50 and etc. When i run the game i cant use the arrow keys to move the hexagon..

    import java.awt.*;
    import javax.swing.*;
    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.*;




public class SuperHexagon extends JFrame {
  public static final int WIDTH = 1440, HEIGHT = WIDTH / 16 * 9;


  public SuperHexagon() {
    setPreferredSize (new Dimension(WIDTH, HEIGHT));
    setMaximumSize (new Dimension(WIDTH, HEIGHT));
    setMinimumSize (new Dimension(WIDTH, HEIGHT));

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setLocationRelativeTo(null);

    setVisible(true);
  }


  public static void main (String[] args) throws IOException {
    JFrame frame = new SuperHexagon();
    int hexX = (WIDTH / 2) - 23, hexY = (HEIGHT / 2) - 23;


    Graphics g = frame.getGraphics();
    BufferedImage bgImage = ImageIO.read(new File("Images/gameBG.png"));
    BufferedImage hexImage = ImageIO.read(new File("Images/hexagon.png"));
    frame.addKeyListener(new KeyInput());

    g.drawImage(bgImage, 0, 0, null);
    g.drawImage(hexImage, hexX, hexY, null);
    g.dispose();



  }
}

The key inputs:

import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;


public class KeyInput extends KeyAdapter {

  /* Coordinates that player can move (3x3 grid)
   * (645,330)(720,330)(795,330)
   * (645,405)(720,405)(795,405)
   * (645,480)(720,480)(795,480)
   */

  public void keyPressed(KeyEvent e, int hexX, int hexY) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_UP) {
      hexY -= 75; 
    }
    if (key == KeyEvent.VK_DOWN) {
      hexY += 75;
    }
    if (key == KeyEvent.VK_RIGHT) {
       hexX += 75;
    }
    if (key == KeyEvent.VK_LEFT) {
      hexX -= 75;
    }
  }

}
Ted Lim
  • 67
  • 7
  • I don't understand how it's a duplicate of "Is java a pass-by-value or pass-by-reference?" – Ted Lim May 16 '16 at 21:45
  • If you have method like `method(int i){i++}` and you use it like `int foo = 1; method(foo);` then `foo` will still be 1. Java is pass by value, which means that you are not passing variable itself, but only value which it holds (`i` argument will copy that value and `i++` will affect only `i` variable, `foo` will be same). This is what is happening in your `keyPressed(KeyEvent e, int hexX, int hexY)` method. With `hexY -= 75;` you are updating *local variables* of `keyPressed` method, not variables which you used to invoke that method. – Pshemo May 16 '16 at 21:48
  • what would I have to do in order for it to change the proper variable? – Ted Lim May 16 '16 at 21:55
  • It depends, but it looks like you could change `KeyInput` class to be into inner of `SuperHexagon`, also declare `hexY` and `hexY` as fields in outer class (SuperHexagon) and use them there. Inner classes have direct access to members of its outer class so it can change its fields (just change names of `keyPressed` method parameters to avoid confusion). – Pshemo May 16 '16 at 21:59
  • I got and error saying: No enclosing instance of type SuperHexagon is accessible. Must qualify the allocation with an enclosing instance of type SuperHexagon (e.g. x.new A() where x is an instance of SuperHexagon). What does this mean? – Ted Lim May 16 '16 at 22:06
  • Well, inner class needs to know which exact instance of outer class should it be able to manipulate. Which is why Inner instances can be only created via `outerInstance.new InnerClass();`. BTW, if you would try to invoke `new InnerClass()` inside non-static code block of `OuterClass` compiler could automatically add `this` as instance of OuterClass. – Pshemo May 16 '16 at 22:12

0 Answers0