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

2018/4/20

X & Y arrays

Miguel.Valdez Miguel.Valdez

2018/4/20

#
How would it possible to make a for loop to populate 2 arrays for my worlds x and y positions(800, 600). Such that the arrary is evenly spaced across the dimension x or y, 10 times? Thanks.
danpost danpost

2018/4/20

#
1
2
3
4
5
6
7
8
9
10
11
12
13
int dy = getHeight/10;
int dx = getWidth()/10;
int y = dy/2;
for (int j=0; j<10; j++)
{
    int x = dx/2;
    for (int i=0; i<10; i++)
    {
        // if array has object at [j][i], addObject at (x, y);
        x += dx;
    }
    y += dy;
}
Miguel.Valdez Miguel.Valdez

2018/4/20

#
Would it be possible to nest a "while" loop into there to make 100 objects (example a apple) even spaced in the world(like a grid)?
danpost danpost

2018/4/20

#
Miguel.Valdez wrote...
Would it be possible to nest a "while" loop into there to make 100 objects (example a apple) even spaced in the world(like a grid)?
Should not need a while loop. Just this at line 9:
1
addObject(new Apple(), x, y);
Miguel.Valdez Miguel.Valdez

2018/4/20

#
I understand and agree with you 100 % Danpost. But what I'm being asked to do is. From the 2 arrays for X & Y position, use the array length as part of a while loop to populate 100 objects (ex: apple) using coordinates generated in from the arrays. I've been going crazy, making a Picasso painting on my whiteboard(from making examples of the code), but can't come up with anything :T
You need to login to post a reply.