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

2012/4/1

What's the meaning of this piece of code please?

poila. poila.

2012/4/1

#
So I have written this code but I don't fully understand how it works. Can anyone please explain to me what it does basically please? THANK YOU! public class Spider extends Animal { /** * Act - do whatever the Lizard wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public static int FlysEaten = 0; public void lookForFly() { if ( canSee(Fly.class) ) { eat(Fly.class); FlysEaten++; } } }
danpost danpost

2012/4/1

#
The 'public static int FlysEaten = 0;' statement will be used to keep track of how many flies that ALL of your spiders have eaten. I believe the 'canSee(Class cls)' and the 'eat(Class cls)' are methods inherited from the Animal class. The 'canSee' will check for intersecting flies with this object, and if there are any, the 'eat' will remove one. Then the counter increments with 'FlysEaten++'.
You need to login to post a reply.