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

2016/6/15

Greenfoot.stop()

Miro29 Miro29

2016/6/15

#
Hi all, I have a question and a help request at the same time. I want to stop the game after the Crab eats the Worm, and for that in the Greenfoot manual I found the Greenfoot.stop(); method. The thing is that I am probably missing something, because no mater where I put it , it stops the game immediately and not when the Lobster eats the Crab. Any tips and suggestions are welcome.
1
2
3
4
5
6
7
public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
}
}
danpost danpost

2016/6/15

#
Miro29 wrote...
I want to stop the game after the Crab eats the Worm, and for that in the Greenfoot manual I found the Greenfoot.stop(); method. The thing is that I am probably missing something, because no mater where I put it , it stops the game immediately and not when the Lobster eats the Crab.
Where did you try to place the 'Greenfoot.stop();' line? Did you place the line in more than one place in your code and forgot to remove one (possibly in a different class)? Is it stopping due to a thrown exception (does the terminal show up with a stack trace)? If it stops immediately after you click on the 'Run' button, you should probably show the code to your World subclass (class that "extends World").
Miro29 Miro29

2016/6/15

#
danpost wrote...
Miro29 wrote...
I want to stop the game after the Crab eats the Worm, and for that in the Greenfoot manual I found the Greenfoot.stop(); method. The thing is that I am probably missing something, because no mater where I put it , it stops the game immediately and not when the Lobster eats the Crab.
Where did you try to place the 'Greenfoot.stop();' line? Did you place the line in more than one place in your code and forgot to remove one (possibly in a different class)? Is it stopping due to a thrown exception (does the terminal show up with a stack trace)? If it stops immediately after you click on the 'Run' button, you should probably show the code to your World subclass (class that "extends World").
Hi , thank you for the reply, I checked if I maybe put it in more than one place, and it is only once in the Lobster. I don't know yet about the second question, as in I don't understand it yet :) And, yes, it stops immediately after I click on the 'Run' button. I did try to change or add the extend to World, but I am not doing something right because it will not accept it. This is how the code looks : _________________________________________________________________________
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Lobster here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Lobster extends Animal
{
    /**
     * Act - do whatever the Lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
     move();
     lookForCrab();
     randomTurn();
     turnAtEdge();
    }
    /**
    * Check whether we have stumbled upon a worm.
    * If we have, eat it. If not, do nothing.
    */
    public void lookForCrab()
   {
    if ( canSee(Crab.class) );
    {
        eat(Crab.class);
        Greenfoot.stop();
    }
   }
    /**
   * This method turns randomly to left or right the moving Crab
   *
   */
   public void randomTurn()
   {
     if ( Greenfoot.getRandomNumber(100) < 10 )
        {
            turn( Greenfoot.getRandomNumber(90)-65 );
        }  
   }
   /**
   * This method turns the Crab if he gets to the edge of the world
   */
   public void turnAtEdge()
   {
    if ( isAtEdge() )
        {
            turn(17);
        
   }
}
Super_Hippo Super_Hippo

2016/6/15

#
Remove the semicolon from line 28.
Miro29 Miro29

2016/6/15

#
Super_Hippo wrote...
Remove the semicolon from line 28.
I feel so stupid now :) but I assume this won't be the first time :) Thank you
Miro29 Miro29

2016/6/18

#
Super_Hippo wrote...
Remove the semicolon from line 28.
Hi, I would like to ask for confirmation on something I hope I understood correctly :)
1
2
3
4
5
6
7
8
public void lookForCrab()
  {
   if ( canSee(Crab.class) );
   {
       eat(Crab.class);
       Greenfoot.stop();
   }
  }
After you showed me where the problem was, i was happy, because I got rid of the problme, but then I asked my self the why question :) Teh conclusion I got to is that the semicolon in my case transformed the if into an instruction and based on that logic, it just executed whatever came next. Can you please tell my if I understood correctly what happened ?
danpost danpost

2016/6/18

#
Miro29 wrote...
After you showed me where the problem was, i was happy, because I got rid of the problme, but then I asked my self the why question :) Teh conclusion I got to is that the semicolon in my case transformed the if into an instruction and based on that logic, it just executed whatever came next. Can you please tell my if I understood correctly what happened ?
It is not that the 'if' is transformed into an instruction. The semi-colon is an empty command by itself. So, the non-bracketed empty command is executed when the 'if' clause is true and either way the statement following the semi-colon is then executed.
Miro29 Miro29

2016/6/19

#
danpost wrote...
Miro29 wrote...
After you showed me where the problem was, i was happy, because I got rid of the problme, but then I asked my self the why question :) Teh conclusion I got to is that the semicolon in my case transformed the if into an instruction and based on that logic, it just executed whatever came next. Can you please tell my if I understood correctly what happened ?
It is not that the 'if' is transformed into an instruction. The semi-colon is an empty command by itself. So, the non-bracketed empty command is executed when the 'if' clause is true and either way the statement following the semi-colon is then executed.
Makes sense, thank you !
You need to login to post a reply.