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

2018/7/27

Re: Unable to loop/Restart Code

idk1234 idk1234

2018/7/27

#
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?
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();

    }
Original Post: https://www.greenfoot.org/topics/60772/0
Super_Hippo Super_Hippo

2018/7/27

#
You set the Off-Image when timer is 100. From there you set it to 200 (line 30), so it is still >=0 (line 19), the image will be set to On the next act cycle. You should only need to change the image when the direction changes.
idk1234 idk1234

2018/7/27

#
So how exactly would I fix this, I'm kind of confused? Just don't want the Crushers to be going download at the same time at the start.
Super_Hippo Super_Hippo

2018/7/27

#
private int pauseTimer = Greenfoot.getRandomNumber(500);
if (pauseTimer > 0)
{
    pauseTimer--;
    return;
}

if (timer == 20)
{
    direction *= -1;
    setImage("Evil Crusher_Off.png");
}
else if (timer == -20)
{
    direction *= -1;
    setImage("Evil Crusher_On.png");
}

setLocation(getX(), getY()+direction*speed);
timer += direction;
if (timer == -20) pauseTimer = 100 + Greenfoot.getRandomNumber(500);
Does this looks like what you want?
danpost danpost

2018/7/28

#
You have the following line near the top of the class code:
private int pauseTimer;
You can give it an initial value -- maybe something like:
private int pauseTimer = Greenfoot.getRandomNumber(600);
You need to login to post a reply.