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

2012/10/8

Adding a small pause/wait?

blahblah26 blahblah26

2012/10/8

#
How can you program something to wait a specific amount of time before it does another task?
Game/maniac Game/maniac

2012/10/8

#
1
2
3
4
5
6
7
8
9
10
Public void method()
{
    Int i=0;
    While(i<10){
        i++;
    }
    if(i=10){
        Method2();
    }
}
Method=the method you want to run Method2=what you want to happen
mjrb4 mjrb4

2012/10/8

#
Game/maniac wrote...
1
2
3
4
5
6
7
8
9
10
Public void method()
{
    Int i=0;
    While(i<10){
        i++;
    }
    if(i=10){
        Method2();
    }
}
Method=the method you want to run Method2=what you want to happen
This neither does what's asked for, nor even compiles! You should at least test code before posting it up on a whim... Public, Int and While should all start with lower case letters, and the question refers to a specific amount of time, not an arbitrary, likely microscopic pause the compiler could remove anyway. In terms of the original question, how specific do you want this time to be? If you want it to be in terms of act cycles , then Greenfoot.delay(). If you want it in terms of milliseconds, then Thread.sleep is your friend - but be aware you'll need to catch the resulting InterruptedException if you go down this route.
danpost danpost

2012/10/8

#
This discussion should have the information you need. Hope it helps.
Game/maniac Game/maniac

2012/10/8

#
mjrb4 wrote...
This neither does what's asked for, nor even compiles! You should at least test code before posting it up on a whim... Public, Int and While should all start with lower case letters, and the question refers to a specific amount of time, not an arbitrary, likely microscopic pause the compiler could remove anyway. In terms of the original question, how specific do you want this time to be? If you want it to be in terms of act cycles , then Greenfoot.delay(). If you want it in terms of milliseconds, then Thread.sleep is your friend - but be aware you'll need to catch the resulting InterruptedException if you go down this route.
I was at school and I posted it on my phone which auto caps things and I can't test it and I only had 5 mins to post it, this is what I have come up with though now I'm at home:
1
2
3
4
5
6
7
8
9
int i=0
public void timer(int time)
{
        i++;
        if(i==time)
            Method2();
        if(i==time+1)
            i=0;
}
Replace Method2 with the method you want to execute
davmac davmac

2012/10/8

#
You need to login to post a reply.