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

2013/10/7

Faster way to place objects?

ShoutToUs ShoutToUs

2013/10/7

#
I'm currently working on a replica of Pac-Man in Greenfoot, and I'm at the point of placing all the pellets in their proper place. I quickly realized that it would take far too long to place them all manually, so I was wondering if there is any way to place a set of objects like the pellets in Pac-Man in a faster or more systematic way. This would greatly accelerate my progress on the scenario. Thanks.
danpost danpost

2013/10/7

#
The most systematic way would be to create a map (a two-dimensional array). It can be of type String, int, or some other type; provided you can represent all possible actors by that type (I used an int array in my Pakman remake). Then iterate through the array and place the appropriate object (or skip if no object) at each location. NOTE: in my Pakman remake, I did not represent the pellets in the array. I placed an even distribution of pellets within the playing area and when other object were placed in the world (Wall, Path, and Pill objects), they would find any intersecting pellets and remove them using code within the 'addedToWorld' method.
MatheMagician MatheMagician

2013/10/7

#
When it comes to pacman, I think the fastest way would be this:
int yOffset = 0;
int xOffset = 0;
int height = 400;
int width = 400;
int distH = 100; //how many pellets in one column
int distW = 100; //how many pellets in each row
for(int i=0;i<distW;i++)
for(int j=0;j<distH;j++)
addObject(new Pellet(),xOffset+(i*width)/distW,yOffset+(j*height)/distH);
You need to login to post a reply.