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

2014/7/16

Loop Problems

ILOVELABRADORS ILOVELABRADORS

2014/7/16

#
Hi, I'm having some problems with loops. I know what they are, but I don't really get how to use them. I am creating a something that a ball is just sitting there and a lobster is roaming around in the scenario, looking for the ball. If it finds the ball, it pushes the ball and the ball moves a bit. I'm supposed to use loops in the codes, but I do not know how. I don't have time now to write the super long codes down now, but I will later. Any help would be appreciated. Thanks.
lordhershey lordhershey

2014/7/16

#
how to use loops? It is not an easy answer, but here is an example if you find something like this in your code:
1
2
3
4
5
6
7
8
9
Step A;
Step B;
Step C;
Step A;
Step B;
Step C;
Step A;
Step B;
Step C;
we are doing the same stuff 3 times in this case, this can be simplified to:
1
2
3
4
5
6
for(int i=0; i < 3; i++)
{
  Step A;
  Step B;
  Step C;
}
What this loop says it, using this variable i as a counter start at zero and count until i is no longer less than 3, on each pass of the loop we execute the sequence
1
2
3
Step A;
Step B;
Step C;
the for loop will execute this sequence 3 times, with i being 0,1,2. But, why not 3? you might ask, well that is due to the i < 3 clause which means execute this if it is true, if not then stop doing this. The i++ in the last part of the for is what happens after a loop is executed. Hope this helps a little bit.
ILOVELABRADORS ILOVELABRADORS

2014/7/19

#
Thanks, but would you also tell me how you can make some words appear on the screen when something happens, so let's say the lobster bumps into the ball, then something will appear in the corner of the screen. Thanks! =D XD :D
danpost danpost

2014/7/19

#
There is a GreenfootImage constructor that takes a String value (text) and creates an image with that text on it. Create the image of the text you want and set that image as the image of an Actor (or draw the image onto the background image of the world) when the two objects intersect.
ILOVELABRADORS ILOVELABRADORS

2014/7/20

#
Thanks.
You need to login to post a reply.