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

2012/8/1

Question, please help!

thatsme thatsme

2012/8/1

#
I am trying to place 10 objects randomly into my world without creating 10 separate method calls. I have the random part, I am just finding difficulty in writing it as one line. Would I need to create a loop or how would this be done? Any help would be greatly appreciated, I am new to programming. Thanks!
thatsme thatsme

2012/8/1

#
I was able to figure this out by using a loop variable.
Morran Morran

2012/8/2

#
Yeah, you use a loop. My favorite to use is the for loop:
for(int index = 0; index < numOfObjectsYoureTryingToCreate; index++) {
   addObject(objectYoureTryingToAdd, randomX, randomY);
}
You can also use a while loop:
int index = 0;
while(index < numOfObjectsYoureTryingToCreate) {
   addObject(objectYoureTryingToAdd, randomX, randomY);
   index++;
}
SPower SPower

2012/8/3

#
thatsme wrote...
using a loop variable.
A loop isn't a variable, it's a statement. You can't do anything like this:
forLoop a = *something*;
that would be really weird!
You need to login to post a reply.