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

2018/2/9

Randomly place objects

Heet_Patel Heet_Patel

2018/2/9

#
Hi but could i have the code for placing objects into the world. There should be at least five of them spawning. My World class is The_House and my actors are Villian and The_Jail. It would be better if I could have seven of The_Jail.
danpost danpost

2018/2/9

#
You can get random coordinates:
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
for each actor you wish to add. You can even create a method to place a given actor anywhere randomly in the world:
public void addActor(Actor actor)
{
    int x = Greenfoot.getRandomNumber(getWidth());
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(actor, x, y);
}
then, for example, add an actor like so:
addActor(new Villian());
or multiple actors like so:
for (int i=0; i<7; i++) addActor(new The_Jail());
Heet_Patel Heet_Patel

2018/2/11

#
public void addActor(Actor actor)

{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot,getRandomNumber(getHeight());
addObject(Villian,x,y)
}
Is this the correct code if i want the villian toget added. Does this code also add random number or just random places.
Super_Hippo Super_Hippo

2018/2/11

#
He gave you the correct codes, you don't need to change them.
danpost danpost

2018/2/11

#
For random number (and random locations):
for (int i=0; i<1+Greenfoot.getRandomNumber(7); i++) addActor(new The_Jail());
This places a minimum of one and a maximum of 7 The_Jail actors into the world at random places.
Super_Hippo Super_Hippo

2018/2/11

#
For same chances to get any number of The_Jail actors (1..7), you can use:
for (int i=1+Greenfoot.getRandomNumber(7); i>0; i--) addActor(new The_Jail());
(At least, as far as I know, the condition will be executed again all the time, so the chance to get 7 Jail_Actors in the world with danpost's code is only 0.612 %.)
danpost danpost

2018/2/11

#
@Hippo, you are correct (I did not think about the repeated execution of the limit check which would change the limit each time). I should have got an absolute limit first:
int count = 1+Greenfoot.getRandomNumber(7);
for (int i=0; i<count; i++) addActor(new The_Jail());
which would also give equal chance.
Heet_Patel Heet_Patel

2018/2/14

#
danpost wrote...
You can get random coordinates:
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
for each actor you wish to add. You can even create a method to place a given actor anywhere randomly in the world:
public void addActor(Actor actor)
{
    int x = Greenfoot.getRandomNumber(getWidth());
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(actor, x, y);
}
then, for example, add an actor like so:
addActor(new Villian());
or multiple actors like so:
for (int i=0; i<7; i++) addActor(new The_Jail());
it says that after the first bracket for the (getHeight()); i need a semi colon(;)
Heet_Patel Heet_Patel

2018/2/14

#
then it says that it cannot find symbol for getWidht and getHeight it also says that t cannotfind the method for addObject
Super_Hippo Super_Hippo

2018/2/14

#
You need to place the method into your world subclass, not an actor subclass. Or you need to add a 'getWorld().' before all used world methods.
Heet_Patel Heet_Patel

2018/2/16

#
ok tnx guys
Heet_Patel Heet_Patel

2018/2/16

#
i got it to work kind o like the way i wanted it to.
You need to login to post a reply.