Reputation: 1
I’m trying to program the game "Gravity Guys", in this game the player runs forward and can change the gravity only when it touches surface. There is an enemy player following him and if he touches the main player the game ends. I want to program this enemy player giving him the same method as main player, but I want the enemy player to make the same moves as the main player after 3 seconds, in this case if the main player is blocked the enemy player touches it and the game will end. Do not know how to replicate this movement after 3 seconds, advice?
public class KeyInput extends KeyAdapter{
Handler handler;
private long startTime = -1; // Impostiamo un valore iniziale non valido
public KeyInput(Handler handler){
this.handler=handler;
}
public void keyPressed(KeyEvent e){
int key=e.getKeyCode(); //ogni comando ha una key
for(int i =0 ; i<handler.object.size(); i++){
GameObject tempObject= handler.object.get(i);
if (tempObject.getId() == ObjectId.Player) {
if (key == KeyEvent.VK_SPACE && ((Player) tempObject).canSwitchGravity ) {
tempObject.setGravityInverted(!tempObject.isGravityInverted());
((Player) tempObject).canSwitchGravity = false;
}
}
if (tempObject.getId() == ObjectId.Enemy){
if (key == KeyEvent.VK_SPACE && ((Enemy) tempObject).canSwitchGravity ) {
if (startTime == -1) {
startTime = System.currentTimeMillis();
}
long elapsedTime = System.currentTimeMillis() - startTime;
if (elapsedTime >= 2900) {
tempObject.setGravityInverted(!tempObject.isGravityInverted());
((Enemy) tempObject).canSwitchGravity = false;
startTime = -1;
}
}
}
if(key== KeyEvent.VK_ESCAPE){
System.exit(1);
}
}
}
This method works that if I press space and a variable that I set in another method is true then the player jumps . I would like the enemy player to do the same but a few seconds later (for example 3s)
Upvotes: 0
Views: 37