I'm making a 2D game in android studio and I've been sitting on a problem for a few days now. I want to stop my thread Gravity, so that my player can jump. When the player is done jumping, the gravity thread can continue.
I searched on the internet for how to use the wait() and notify() with the syncronised block. But when I try to apply it, my Gravity won't stop. If someone can show me how to use this in the correct way, I would be overjoyed...
This is my code in my scene class to start the jumping.
public void recieveTouch(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
if(player.getPoint().y > 0) {
playerJump();
}
}
}
public void playerJump(){
playerJump = new Jump(player, this);
thread = new Thread(playerJump);
thread.setDaemon(true);
thread.start();
}
This is my Gravity thread.
public class Gravity implements Runnable {
private Player player;
private GameScene gameScene;
public Gravity (Player player, GameScene gameScene){
this.player = player;
this.gameScene = gameScene;
}
@Override
public void run(){
while(true){
player.playerMoveDown(3);
gameScene.update();
try {
Thread.sleep(25);
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
And this is my Jump thread.
public class Jump implements Runnable {
private Player player;
private GameScene gameScene;
public Jump (Player player, GameScene gameScene){
this.player = player;
this.gameScene = gameScene;
}
@Override
public void run(){
int eindHoogte = player.getPoint().y - 60;
while (player.getPoint().y > eindHoogte) {
player.playerMoveUp(3);
gameScene.update();
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}