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


Greenfoot code

How to create a while loop method to create and add three (3)Enemyobjects to the world in a vertical line. The starting position of the first Enemy object should be set to 20 pixels from the right and 100from the top of the World. Make y increase by 40 pixels for each new Enemy object created.
RcCookie RcCookie


for(int i=0; I<3; i++) addObject(new Enemy(), 20+40*i, 100);
what code would be there if it was said to be done in a horizontal line in while loop and x said to be increased by 40 instead of y
danpost danpost


To turn a for loop into a while loop, break the components up as follows:
// from
for (int i=0; i<3; i++)

// to
int i = 0;
while (i < 3)
The code given by RcCookie (with typo "I" should be "i") is for horizontal line of actors. Just switch the last two parameters in the addObject command to change to a vertical line.
so i should just need to put as (100, 40*i, 20) or (20, 100, 40*i)
Thank you so much dan I got it
You need to login to post a reply.