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

2012/4/8

Create delay for one Actor

SPower SPower

2012/4/8

#
Hi all, I want a object to be created, and a few second later, another object is created. There is a delay method in Greenfoot, but when I use that, my whole scenario will stand still for a few seconds. Is there a posibility to create a delay for one Actor?
-nic- -nic-

2012/4/8

#
hmm that sound like something i need to but i had the same problem!!
SPower SPower

2012/4/8

#
I came up with a solution myself, so you don't have to give me solutions anymore.
Reck Reck

2012/9/6

#
Share it! :-)
SPower SPower

2012/9/6

#
Easy or complex version?
MatheMagician MatheMagician

2012/9/6

#
One method is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
long pauseTime = 0;//saves the time when we can start moving again
    /**
     * Act - do whatever the Dudette wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if(running())
            // Add your action code here.
        if(Greenfoot.isKeyDown("p"))
            pause(1000);//pause game for 1000
    }   
    public boolean running() //are we running or are we paused
    {
        long time = System.currentTimeMillis();
        return time > pauseTime;
    }
    public void pause(long delay) //delay represents how long we are delaying for
    {
        long time = System.currentTimeMillis();
        pauseTime = time+delay;//redefine pauseTime so it is in the future-exactly delay milliseconds in the future
    }
which I used in my pause scenario.
SPower SPower

2012/9/6

#
@MatheMagician I think he asked me....
SPower SPower

2012/9/6

#
And this discussion is about a delay for a single Actor, not for the whole game....
SPower SPower

2012/9/6

#
Let me just tell the 2 options: Simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// in a subclass of Actor:
private int delay;
private static final int MAX_DELAY = 20; // or whatever works.
// The higher this number, the longer the delay.
 
public void act()
{
    if (delay < MAX_DELAY) {
        delay++;
        return;
    } else {
        delay = 0;
    }
    // other stuff
}
Complex: (although not super complex)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// in a subclass of Actor
private long curTime, prevTime;
private static final long DELAY_TIME = 1000; // in milliseconds
 
public void act()
{
    curTime = System.currentTimeMillis();
    if (prevTime +DELAY_TIME > curTime) {
          return;
    } else {
        prevTime = curTime;
    }
      // other stuff
}
The only difference is that the second options uses real time (that's why you enter milliseconds in DELAY_TIME), and the first is relative to the speed of your scenario. Choose which one is useful for you.
Gevater_Tod4711 Gevater_Tod4711

2012/9/6

#
Im not sure if this only stops one object but maybe it helps:
1
2
3
4
5
6
try {
     Thread.currentThread().sleep(theTimeYouWantInMilliseconds);
}
catch(InterruptedException ie) {
    ie.printStackTrace(); //only if you want the exception to be printet on the terminal;
}
MatheMagician MatheMagician

2012/9/6

#
My code does only pause one actor. I do not know what made you think otherwise. Also, I thought the only reason he asked you was because you were the only person he knew did know how to do it. I thought my code was relevant and necessary. Also, I am not sure how my comment could have inconvenienced anyone, but if it did I am sorry. P.S. I am not sure if you noticed, but my code is almost exactly like your complex code, except I built in a method to pause it as much and whenever you want.
danpost danpost

2012/9/6

#
How about this:
1
2
3
4
5
6
7
8
9
10
// instance variable
int delay = 20; // or whatever (or 0 if no initial delay)
// first two lines in act
if (delay > 0) delay--;
if (delay > 0) return;
// optional method to initiate a delay
public void setDelay(int delayAmt)
{
    delay = delayAmt;
}
SPower SPower

2012/9/7

#
Sorry MatheMagician, I didn't read your code really good.
You need to login to post a reply.