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

2013/9/14

Help with spawning???

Dethace Dethace

2013/9/14

#
I have an assignment using Trick the Turtle, where I must currently spawn random numbers of lettuce at random locations, while also spawning a turtle, two snakes, and a bug at random locations as well. However, when I try to spawn the turtle, snakes, and bug, they all spawn at the same location. Could someone examine my code and tell me what I'm doing wrong? public class TurtleWorld extends World { /** * Create the turtle world. Our world has a size * of 560x460 cells, where every cell is just 1 pixel. */ public TurtleWorld() { super(600, 480, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Counter myCounter = new Counter(); addObject(myCounter, 50, 30); int numberOfLettuce=Greenfoot.getRandomNumber(15)+1; int count=1; while (count<=numberOfLettuce) { Lettuce newLettuce = new Lettuce(); int randomX=Greenfoot.getRandomNumber(getWidth()); int randomY=Greenfoot.getRandomNumber(getHeight()); addObject(newLettuce, randomX, randomY); count++;; } int randomX=Greenfoot.getRandomNumber(getWidth()); int randomY=Greenfoot.getRandomNumber(getHeight()); Snake snake = new Snake(); addObject(snake, randomX, randomY); Bug bug = new Bug(); addObject(bug, randomX, randomY); Snake snake2 = new Snake(); addObject(snake2, randomX, randomY); Turtle turtle = new Turtle(); addObject(turtle, randomX, randomY); } }
danpost danpost

2013/9/14

#
The big difference between the spawning of the lettuce (which IS spawning at random locations) and the spawning of the other actors is the 'while' loop which re-assigns new values to the 'randomX' and 'randomY' fields on each pass through the loop. When spawning the others, you are using the same 'randomX' and 'randomY' values that are assigned right after the loop.
Dethace Dethace

2013/9/14

#
Then would you have to put each new object into their own separate spawn loops, or is there a way to make each one spawn at different locations with just one or two extra lines of code?
danpost danpost

2013/9/14

#
No; you do not need more loops. Just re-assign 'randomX' and 'randomY' between (or before) each spawning. You will not need the 'int' qualifier on subsequent assignments.
Dethace Dethace

2013/9/14

#
Re-assign between or before the spawning? I'm very sorry If I'm coming off as dumb, but may I ask how you do that? I'm new to the programming thing, so this is all very foreign to me.
danpost danpost

2013/9/14

#
Adding a new object into the world is what is called 'spawning', which is what you are doing with these paired lines of code (one spawning per pair).
Snake snake = new Snake();
addObject(snake, randomX, randomY);

Bug bug = new Bug();
addObject(bug, randomX, randomY);

Snake snake2 = new Snake();
addObject(snake2, randomX, randomY);

Turtle turtle = new Turtle();
addObject(turtle, randomX, randomY);
You already have the following before the first pair:
int randomX=Greenfoot.getRandomNumber(getWidth());
int randomY=Greenfoot.getRandomNumber(getHeight());
You need to re-assign them before the rest of them with (at lines 3, 6 and 9):
randomX=Greenfoot.getRandomNumber(getWidth());
randomY=Greenfoot.getRandomNumber(getHeight());
Dethace Dethace

2013/9/14

#
I kept putting INT before hand, and that's why teh code wasn't working Thank you!
You need to login to post a reply.