Java在另一个类中更改静态变量不会影响对象值

时间:2019-01-28 09:22:36

标签: java static libgdx

我有一个要更改的变量SCROLL_SPEED

此变量为static,并在以下类中定义:

public class ScrollHandler {

    public static int SCROLL_SPEED = -57;
    public final int TOWER_GAP = 200;

    private GameWorld gameWorld;

    public ScrollHandler(GameWorld gameWorld, float yPos) {
        this.gameWorld = gameWorld;

        GameWorld.obstacle1 = new Obstacle(210, 0, 25, 0, SCROLL_SPEED, yPos);
        GameWorld.obstacle2 = new Obstacle(GameWorld.obstacle1.getTailX() + TOWER_GAP, 0, 25, 0, SCROLL_SPEED, yPos);
        GameWorld.obstacle3 = new Obstacle(GameWorld.obstacle2.getTailX() + TOWER_GAP, 0, 25, 0, SCROLL_SPEED, yPos);
        GameWorld.obstacle4 = new Obstacle(GameWorld.obstacle3.getTailX() + TOWER_GAP, 0, 25, 0, SCROLL_SPEED, yPos);
    }

    public void update(float delta) {
        // Update our objects
        GameWorld.obstacle1.update(delta);
        GameWorld.obstacle2.update(delta);
        GameWorld.obstacle3.update(delta);
        GameWorld.obstacle4.update(delta);

        // Check if any of the obstacles are scrolled left,
        // and reset accordingly
        if (GameWorld.obstacle1.isScrolledLeft()) {
            GameWorld.obstacle1.reset(GameWorld.obstacle4.getTailX() + TOWER_GAP);
        } else if (GameWorld.obstacle2.isScrolledLeft()) {
            GameWorld.obstacle2.reset(GameWorld.obstacle1.getTailX() + TOWER_GAP);
        } else if (GameWorld.obstacle3.isScrolledLeft()) {
            GameWorld.obstacle3.reset(GameWorld.obstacle2.getTailX() + TOWER_GAP);
        } else if (GameWorld.obstacle4.isScrolledLeft()) {
            GameWorld.obstacle4.reset(GameWorld.obstacle3.getTailX() + TOWER_GAP);
        }
    }

当我通常将SCROLL_SPEED更改为public static int SCROLL_SPEED = -285时,它可以正常工作。

但是当我将SCROLL_SPEED从如下所示的另一个类更改时,它不起作用(值仍然相同):

try {
        turbomode = new Rectangle(GameWorld.obstacle2.getX() - GameRenderer.generator2.getValue2(),
                GameWorld.obstacle2.getY() + GameRenderer.generator2.getValue1(), 15, 15);
        if ((Intersector.overlaps(GameWorld.wizard.getBoundingRectangle(), turbomode))){
            GameRenderer.TurboModeActive = true;
            activeItem = true;
            case1 = true;
            if (activeItem){
                System.out.println("TEST 1");
                ScrollHandler.SCROLL_SPEED = -285;
            } else{
                ScrollHandler.SCROLL_SPEED = -57;
            }
            System.out.println("TEST 2");
            new java.util.Timer().schedule(
                    new java.util.TimerTask() {
                        public void run() {
                            GameRenderer.TurboModeActive = false; 
                            activeItem = false;
                            case1 = false;
                        }
                    },
                    8000
            );
        }
} catch (NullPointerException e){
    System.out.println("TEST 3");
}

我知道它会通过if语句,因为它会打印出TEST 1,但是我不知道为什么它不起作用。有人可以告诉我这个问题是什么吗?

2 个答案:

答案 0 :(得分:3)

这是一个计时问题。

GameWorld.obstacle1 = new Obstacle(210, 0, 25, 0, SCROLL_SPEED, yPos);

如果在创建SCROLL_SPEED之后更改obstacle1,则将不再起作用。那个障碍已经有价值。

因此,您需要找到障碍物实例并更改其速度实例变量,或者让障碍物查看您的静态变量而不是其自己的变量。

答案 1 :(得分:0)

如果您想更改 SCROLL_SPEED 并影响所有障碍物,只需从障碍物 SCROLL_SPEED 中删除< / strong>构造函数,并在需要时直接从障碍调用 ScrollHandler.SCROLL_SPEED

所以

GameWorld.obstacle1 = new Obstacle(210, 0, 25, 0, SCROLL_SPEED, yPos);

成为

GameWorld.obstacle1 = new Obstacle(210, 0, 25, 0, yPos);

并在Obstacle构造函数中调用 ScrollHandler.SCROLL_SPEED