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

2012/9/25

How do i use sleep()?

frederikam frederikam

2012/9/25

#
The title says it all.
plcs plcs

2012/9/25

#
What are you aiming to use sleep for? If you want to stop your Greenfoot scenario for a brief amount of time you can consider it. But it makes the whole of Greenfoot pause for the period of time you've set, not just a particular actor/world. This is unlikely to be the behaviour you want. But in the honour of meeting your thread title.
Thread.sleep(500) // sleep 500 milliseconds.
Set the amount of milliseconds you want to sleep, and place this in your code at the appropriate line.
frederikam frederikam

2012/9/25

#
Just a single actor. I am trying to animate the wombat in the wombat scenario.
    public Wombat(){
        while(dead == false){
           Thread.sleep(5000);
           setImage("wombat2");
           Thread.sleep(1000);
           setImage("wombat");
        }
But i get an error. I am a newbie BTW.
danpost danpost

2012/9/25

#
I do not understand what 'sleep' has to do with animating an actor, unless you are trying to slow down the alternating of the images. If that is the case, just add an instance 'int counter' to the class, use 'counter = (counter + 1) % 4;' in the act method (or a method is calls) and each time counter equals zero, change the image. The use of '4' for the cycle count was arbitrary and can be adjusted to suit. You could actually double (since you have two images) the cycle count amount used ('8' instead of '4' in this case) and use the value of 'counter % 4' to determine if the image is to change (when equal to zero) and use the value of 'counter / 4' (which will return one or zero) to determine the image to use. Hope this helps.
frederikam frederikam

2012/9/25

#
I do not know how i else could animate an actor. I have been working with greenfoot in less than a day, can you explain what you are saying? I want to know how i can have it stop reading down the code, before it has waited some seconds, or whatever delay i want.
danpost danpost

2012/9/25

#
You need to understand how the execution of OOP works. Within Greenfoot, the world constructor will run during a 'Reset' (which is also run after compilation). When the project is started, the world and each actor will cycle through there act methods. Each cycling is called a frame. If you 'stop reading down the code' in one actor, you are stopping all actors and the world from doing anything. What you would want to do, is have the actor do something only after so many frames have passed, which allows the world and other actors their timely cycling of acts. In order to accomplish this, you will need an instance field to count the frames and then do what you need to do every so many frames (instead of each frame). The counter will increment every frame, and an 'if' statement is used to see if the counter is a certain value. When it is, the counter is zeroed and the action is executed.
counter++;
if (counter == 4)
{
    counter = 0;
    // do something
}
Another way of coding it is
counter = (counter + 1) % 4;
if (counter == 0) { //* do something */ }
danpost danpost

2012/9/25

#
I noticed that your filenames do not show the extensions (".png", "jpg", or other) which will be needed.
davmac davmac

2012/9/25

#
Also, If you really do want to pause execution of the whole scenario, use Greenfoot.delay(...) instead of Thread.sleep(...).
frederikam frederikam

2012/9/25

#
Thx for the help, would try it.
frederikam frederikam

2012/9/25

#
What does "% 4" do? And does this work?
while(x == true){
   counter = (counter + 1) % 4;
   if (counter == 0) {
      counter = (counter + 1) % 4;
      if (counter == 0) {something new}}}
danpost danpost

2012/9/25

#
The '%' operation is 'remainder after division'. 'counter = (counter + 1) % 4;' is equivalent to
counter = counter + 1;
while (counter >= 4) counter = counter - 4;
So, on consecutive cycles the value of 'counter' will go 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, etc. Your code with the 'while' statement will not work in the manner you would want. Certainly, line 5 will always return false.
You need to login to post a reply.