简体   繁体   中英

Java same variable has different values in different

So I have a class which handles user input to a screen created as part of a libgdx application. My problem is that the variable cameraDelta, which is a Vector2, appears to have a different value in keyTyped and getCameraDelta.

using a sys.out.println at runtime in both methods shows the value changing whenever any of 'wasd' are pressed, and the value stays over time, yet the value always stays at (0, 0) when outputted from getCameraDelta

public class InputHandler implements InputProcessor {

    private Firetruck myTruck;
    private GameWorld myWorld;
    private int mouseX;
    private int mouseY;
    private Vector2 cameraDelta;

    public InputHandler(GameWorld myWorld) {
        this.myWorld = myWorld;
        myTruck = myWorld.getFiretruck();
        cameraDelta = new Vector2(0, 0);
    }

    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        if (character == 'w') {
            cameraDelta.add(0, 5);
        } else if (character == 's') {
            cameraDelta.add(0, -5);
        } else if (character == 'd') {
            cameraDelta.add(5, 0);
        } else if (character == 'a') {
            cameraDelta.add(-5, 0);
        }
        return false;
    }

    public Vector2 getCameraDelta() {
        /*/
        Vector2 temp = this.cameraDelta.cpy();
        /*/
        Vector2 temp;
        temp = new Vector2(0, 0);
        temp.add(cameraDelta);
        Gdx.app.log("getCameraDelta", cameraDelta.toString());
        cameraDelta.x=0;
        cameraDelta.y=0;
        return temp;
    }

This is because you're adding in your new Vector2 instance an object cameraDelta . Objects in java are passed by reference, so whenever you change something to your cameraDelta it will change everywhere where is used. For example :

public class Foo {
  public int fooNumber;
}

public class Bar {
  public Foo fooBar;

  public Bar() {
      fooBar = new Foo();
      fooBar.fooNumber = 1;
  }

  public void changeReference(Foo foo) {

      System.out.println(fooBar.fooNumber);  // RESULT -> 1

      fooBar = foo;  // here you're making your original foo to
      //point to the "foo" passed in the method

      foo.fooNumber = 0;

      System.out.println(fooBar.fooNumber); // RESULT -> 0
  }

  public static void main(String[] args) {
      Bar bar = new Bar();
      Foo foo = new Foo();
      bar.changeReference(foo);
      System.out.println(bar.fooBar.fooNumber); // RESULT -> 0
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM