简体   繁体   中英

Java changing static variable in another class doesn't affect object value

I have a variable SCROLL_SPEED that I want to change.

This variable is static and defined in the following class:

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);
        }
    }

When I change SCROLL_SPEED normally to public static int SCROLL_SPEED = -285 , it works perfectly fine.

But when I change SCROLL_SPEED from another class like the following, it doesn't work (the value is still the same):

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");
}

I know that it goes through the if statement, because it prints out TEST 1 , but I don't know why it does not work. Can someone tell me what the issue might be?

It is a timing issue.

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

If you change SCROLL_SPEED after obstacle1 has been created, that won't have any effect anymore. That obstacle already has its value.

So you need to find the obstacle instance and change its speed instance variable, or have the obstacle look at your static variable instead of its own variable.

If you want to change the SCROLL_SPEED and affect all Obstacle , you just have to remove the SCROLL_SPEED from the Obstacle constructor and call directly ScrollHandler.SCROLL_SPEED from Obstacle when you need it.

So

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

become

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

and in Obstacle constructor call ScrollHandler.SCROLL_SPEED

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