Hi, I had previously made a discussion as I did not know how to make my "Crusher" class object go up and down randomly. My code was revised and it works to some extent:
1. As I have multiple "Crushers" they go down at the same time as soon as I run the world, when rather they should be randomly going up and then down (random up, down code works, after they initially go down and return back up at the same time.
2. I intended on having an image for then the Crusher goes back up("Evil Crusher off.png") and one for when the Crusher goes down ("Evil Crusher on.png"), however the "off" image is only there at the start (when the crushers go down togther) for a split second, and then it stays as the "on" image.
Help please?
Original Post: https://www.greenfoot.org/topics/60772/0
public class Crush extends Actor
{
private int timer ;
private int speed = 5; // adjust as needed
private int direction = -1;
private int pauseLimit = 30;
/**
* Act - do whatever the Crush wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public void crush()
{
timer += direction;
if (timer >= 0)
{
setLocation(getX(), getY()+direction*speed);
setImage("Evil Crusher_On.png");
}
if (timer == 100 && direction == 1) // adjust as needed
{
//pauseLimit = 100 + Greenfoot.getRandomNumber(505); // adjust as needed
setImage("Evil Crusher_Off.png");
direction = -1;
speed = 2;//
timer = 200;//
}
if (timer == -pauseLimit)
{
timer = 0;
direction = 1;
speed = 4; //
pauseLimit = 100 + Greenfoot.getRandomNumber(505); // adjust as needed
}
touch();
}

