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

2018/3/14

delay

doglover555 doglover555

2018/3/14

#
How can I delay just one object (not the entire program with Greenfoot.delay())? I have a parent class and then four subclasses that will all use this delay method at some point.
Yehuda Yehuda

2018/3/14

#
You can make the act method not run.
1
2
3
4
public void act() {
    if (delay) return; // if some delay boolean is true, exit method
    ...
}
doglover555 doglover555

2018/3/15

#
how could it continue after a few seconds though?
danpost danpost

2018/3/15

#
doglover555 wrote...
How can I delay just one object (not the entire program with Greenfoot.delay())?
No -- Greenfoot.delay(int) will cause everything to be put on hold for the duration of the delay. It does not cause the method the delay is called from to be exited. It just puts the next line of execution in your program (or the return from the method, if the delay is the last action in the method) to be put on hold -- and the method must be returned from before other act methods can be executed. Yehuda has the correct idea, but was quite vague (and unclear as to what was needed). An int field will be useful to time the delay (by having it count down act cycles). A "few" seconds will probably be around 165-200 act cycles, so you might have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// field
int delayTimer;
 
// act method
public void act()
{
    if (delay > 0 && --delay > 0) return; // do nothing if delaying
    //
    if (<< some condition >>)
    {
        delay = 180; // initiate delay
        // other actions
    }
    //
}
doglover555 doglover555

2018/3/15

#
The problem is I need to use the delay in a method other than act
Yehuda Yehuda

2018/3/15

#
The act method is the only one that needs to be paused, since all other methods will only run if they are called upon by the act method (besides for the constructor method during construction, and other methods that get called by Java or Greenfoot, and are meant to be overridden such as addedToWorld).
doglover555 doglover555

2018/3/15

#
I have a method that changes an object's color, then removes it from the screen, so I need a delay in order for the object's new color to show for a few seconds before removing it from the screen.
Yehuda Yehuda

2018/3/15

#
Do as danpost said. Have an int variable set to the amount of act cycles you want to have this Actor stay before removing it. Each act cycle from when you changed the color decreases the variable by one. Only remove the Actor when the variable gets to 0.
danpost danpost

2018/3/15

#
Replace line 7 with this:
1
2
3
4
5
if (delay > 0)
{
    if (--delay == 0) getWorld().removeObject(this);
    return;
}
doglover555 doglover555

2018/3/15

#
Why doesn't a method like this work?
1
2
3
4
5
6
public void removeDelay()
    {
        int delayTime = 1000;
        while(delayTime > 0) delayTime--;
        if(delayTime == 0) getWorld().removeObject(this);
    }
danpost danpost

2018/3/15

#
doglover555 wrote...
Why doesn't a method like this work?
1
2
3
4
5
6
public void removeDelay()
    {
        int delayTime = 1000;
        while(delayTime > 0) delayTime--;
        if(delayTime == 0) getWorld().removeObject(this);
    }
The 'while' loop keeps execution in the method until 'delayTime' becomes zero. The 'if' line will always have a 'true' condition anytime the method is called. So, it is equivalent to:
1
2
3
4
public void removeDelay()
{
    getWorld().removeObject(this);
}
doglover555 doglover555

2018/3/15

#
Is there a way to do a method like that though or should I use a way from above?
danpost danpost

2018/3/16

#
doglover555 wrote...
Is there a way to do a method like that though or should I use a way from above?
It is possible, but you may still have to modify the act method due to the removal of the actor from the world. The code would still be like what I gave above with the modified line 7-- probably with and 'else if' for initiating the timer.
You need to login to post a reply.