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

2015/12/11

Help me! Spawning random object

mabzstarr mabzstarr

2015/12/11

#
Hi im needing help. I have a project whereby whereby i have a a car and some walls whenever the car touches the wall the game has to stop. What i have so far is the car stopping when it touches the wall. but what i need to also do is have a random object inputted into the scenario and when i click compile the object has to spawn in a random place each time (i dont know how to make the object spawn in a random place) and also if the car touches this object the game has to stop. Please Help. < my scenario
danpost danpost

2015/12/11

#
The description of your issue, as you gave it, does not make any sense. The only time you need to 'compile' your project is when the code itself is altered. Maybe you meant to say that anytime the project is reset, the object is spawned at a random location. This can easily be done with the following lines of code placed in the constructor of your world class:
1
2
3
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new ActorClassName(), x, y);
mabzstarr mabzstarr

2015/12/13

#
oh okay thank you i understand. However I have 1 question what does "int X" and "int Y" code do because my friend suggested i input this code
1
addObject(new Barrel(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
but the end result is the same as the code you have suggested whereby the object spawns at a random location every time project is reset. Does it really matter or they the same. Thanks
danpost danpost

2015/12/13

#
mabzstarr wrote...
... but the end result is the same as the code you have suggested whereby the object spawns at a random location every time project is reset. Does it really matter or they the same. Thanks
They are essentially the same.
You need to login to post a reply.