I know this is a stupid question, but I cannot get my head around this and I have spent a good hour searching online for results to no avail.
I am making a game where enemies move side to side on X-axis at random speeds between 1-5. I have set them up to change direction upon hitting the walls, which worked fine on fixe integers, but does not seem to work on random number.
Upon hitting the rightmost wall, WIDHT-32, they bounce back and maintain their set speed. However, when moving x < 0, they don´t bounce back and leave the screen.
What is the problem?
I have used the following code;
public int speed = (randomno.nextInt(4)+1);
public Enemy(int x, int y) {
super(x, y);
}
public void update(){
x+=speed;
if(x > MainClass.WIDTH-32){
// This works
speed = -speed;
}
if(x < 0){
// Neither of these do....
// Tried A
speed = -(-speed);
// Tried B
speed = speed;
}
}
Aucun commentaire:
Enregistrer un commentaire