简体   繁体   English

Java相同的变量在不同的地方有不同的值

[英]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.所以我有一个类来处理用户输入到作为 libgdx 应用程序的一部分创建的屏幕。 My problem is that the variable cameraDelta, which is a Vector2, appears to have a different value in keyTyped and getCameraDelta.我的问题是变量cameraDelta(Vector2)在keyTyped 和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在运行时在这两种方法中使用 sys.out.println 显示每当按下任何“wasd”时值都会发生变化,并且该值会随着时间的推移而保持不变,但从 getCameraDelta 输出时该值始终保持在 (0, 0)

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 .这是因为您要在新的Vector2实例中添加一个对象cameraDelta Objects in java are passed by reference, so whenever you change something to your cameraDelta it will change everywhere where is used. Java 中的对象是通过引用传递的,因此每当您对cameraDelta更改某些内容时,它都会在使用的任何地方更改。 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
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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