This site requires JavaScript, please enable it in your browser!
Greenfoot back
danteinferno
danteinferno wrote ...

2018/2/27

Need help to rotate a spider

danteinferno danteinferno

2018/2/27

#
So I need to create a constructor with no arguments that ensures that the Spider that is going into the world starts by facing down and to wait a random delay before it starts moving
Vercility Vercility

2018/2/27

#
import java.util.Random

int timer;

public void act() {

Random rand = new Random();
timer = rand.nextInt(Upperbound) + lowerbound ;

while(timer > 0) {
timer--;
}
// your code for moving 

}
danteinferno danteinferno

2018/2/27

#
It can't be in the act method. It needs to be a constructor
danteinferno danteinferno

2018/2/27

#
A no argument, default constructor that ensures that this Spider starts by facing down (a 90 degree turn from its normal orientation) and is set up to wait a random delay of between 25 to 375 calls to act before it starts moving.
Vercility Vercility

2018/2/27

#
The constructor is only supposed to call the 90° turn, the random movement itself has to be in act in order to work after "25 to 375 calls to act()"
import java.util.Random
 
int timer;

public Spider(){
rotate(90);
}
 
public void act() {

Random rand = new Random();
timer = rand.nextInt(350) + 25;

if( timer > 0) {
timer--;
}
else {
// your code for moving  
}
}
danpost danpost

2018/2/28

#
The timer needs set in the constructor and runs in the act method. Also, it is ''turn' -- not 'rotate'.
Vercility Vercility

2018/2/28

#
Oops. Right
You need to login to post a reply.