0

I have my deltaTime loop ,in a MultiThreading program, that returns this void:

public static void update(double deltaTime){
    // System.out.println("I am here");
    if(Key.W){
        Player.y -= deltaTime * Player.speed;
        System.out.println("Holding W | Adding : " + (deltaTime * Player.speed));
    }
}

The program is simple , to check if 'W' is hold and then ,if true, change players Y coordinates.

When I hold W , i get visual feedback that I am holding it , but the code from if statement (Key.W is TRUE for sure) is not being executed.

But it is not working unless i remove '//' from second line of code:

public static void update(double deltaTime){
    System.out.println("I am here");
    if (Key.W) {
        Player.y -= deltaTime*Player.speed;
        System.out.println("Holding W | Adding : " + (deltaTime * Player.speed));
    }
}

Now it is working ... I get no errors in both scenarios , everything else is working :/

What can be the case of this weird bug ?

============================================

I got it working thanks to Andy Turner (https://stackoverflow.com/users/3788176/andy-turner)

If you have the same issue this helped me :

My variable Key.W was declared like this :

Public static boolean W = false;

And to make it work I had to change Public to Volatile like this :

volatile static boolean W = false;

To learn more about why it happens check this link :

Loop doesn't see value changed by other thread without a print statement

0 Answers0