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

2012/5/3

Help !! "for" loop and non random object locations.

tcarrigan tcarrigan

2012/5/3

#
I am stuck....I am using a loop to populate the world....I get using the Greenfoot.getRandomNumber() method to place object in random locations...one of our assignments is to place them evenly space horizontally...for the life of me I cannot figure out how to use it in conjunction with a for loop....all of mine are stacked on each other...any ideas??
SPower SPower

2012/5/3

#
If you think about what you want, you'll see that every object must have the same y coordinate. If you want that to be random, do this:
int y = Greenfoot.getRandomNumber(getHeight());
for (int i = 0; i < limit; i++) {
       addObject(new Object(), Greenfoot.getRandomNumber(getWidth()), y);
}
Else, do this:
for (int i = 0; i < limit; i++) {
       addObject(new Object(), Greenfoot.getRandomNumber(getWidth()), 300);
}
You can replace 300 by the y coordinate you want yourself of course :)
davmac davmac

2012/5/3

#
SPower, I think the question was about how to place the items with even (not random) horizontal spacing. tcarrigan, you need the position to be dependent on the loop index - so something like this:
for (int i = 0; i < 10; i++) {
    addObject(new MyActor(), i * 10, 50);
}
That is, take the loop index 'i' (which will loop through the values 0..9) and multiply it by 10, and use that as the actor's horizontal position.
Duta Duta

2012/5/3

#
davmac wrote...
SPower, I think the question was about how to place the items with even (not random) horizontal spacing. tcarrigan, you need the position to be dependent on the loop index - so something like this:
for (int i = 0; i < 10; i++) {
    addObject(new MyActor(), i * 10, 50);
}
That is, take the loop index 'i' (which will loop through the values 0..9) and multiply it by 10, and use that as the actor's horizontal position.
Or you could just do:
for(int x = 0; x < 100; x += 10)
    addObject(new AnActor(), x, 50);
In a more extended form for clarity:
int startX = 0,
	numActors = 10,
	xSpacing = 10,
	y = 50,
	limit = numActors*xSpacing;
for(int x = startX; x < limit; x += xSpacing)
	addObject(new AnActor(), x, y);
tcarrigan tcarrigan

2012/5/3

#
thank you for your reply...i tried it and yes they are not stacked on one another, thank you, but how do you get them to be evenly spaced apart ?? my world is super(560,560,1); Thanks again for the help....T.
danpost danpost

2012/5/3

#
If you want them to be evenly spaced across the screen
int numberOfActors = 10; // or however many
int spacing = 560 / numberOfActors;
for (int i = 0; i < numberOfActors; i++)
{
    addObject(new MyActor(), spacing / 2 + i * spacing, 280);
}
Duta Duta

2012/5/3

#
tcarrigan wrote...
thank you for your reply...i tried it and yes they are not stacked on one another, thank you, but how do you get them to be evenly spaced apart ?? my world is super(560,560,1); Thanks again for the help....T.
Did you read mine or davmac's replies?
tcarrigan tcarrigan

2012/5/4

#
I think I got it....makes sense now....thank all of you for your help ..I really appreciate it....T
You need to login to post a reply.