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

2014/2/6

Placing objects in world in a specific region

Otaku_Programmer Otaku_Programmer

2014/2/6

#
I'm working on the Asteroids scenario for CS class. Asteroids are only created in the top left corner of the world. I need to Modify the addAsteroids method so that Asteroids are placed within a band along the edge of the world that is 25% of the total height (top and bottom) and 25% of the total width (left and right). I can't for the life of me figure out what to do. Help please.
erdelf erdelf

2014/2/6

#
I think this should work
int x, y = 0;
switch(Greenfoot.getRandomNumber(2))
{
   case 0: x = Greenfoot.getRandomNumber(getWidth()/8); break;
   case 1: x = Greenfoot.getRandomNumber(getWidth()/8)+getWidth()/8*7;
}
switch(Greenfoot.getRandomNumber(2))
{
   case 0: y = Greenfoot.getRandomNumber(getHeight()/8); break;
   case 1: y = Greenfoot.getRandomNumber(getHeight()/8)+getHeight()/8*7;
}
addObject(new Asteroid(), x, y);
Otaku_Programmer Otaku_Programmer

2014/2/6

#
It keeps telling me "variable x might not have been initialized".
erdelf erdelf

2014/2/6

#
oh sry, replace line 1 with
int x = 0;
int y = 0;
Otaku_Programmer Otaku_Programmer

2014/2/6

#
Works perfectly thanks so much.
You need to login to post a reply.