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

2018/2/7

Randomly place objects

Heet_Patel Heet_Patel

2018/2/7

#
Hi, I want a code which can help me place actors randomly when I press run. I have the code for how to to place objects in one particular area every time but now I would like the code for placing actors randomly so that they spawn any where. Thanks for your help guys.
Heet_Patel Heet_Patel

2018/2/7

#
Also is there a online version of green foot where you can create a game on its website or do you have to download it on every computer you have. This is only because my school's library computers don't have the app downloaded and if I want to download it I need to have the administrator's password which I don't have and he won't give it to me. Thanks
danpost danpost

2018/2/7

#
Heet_Patel wrote...
Hi, I want a code which can help me place actors randomly when I press run. I have the code for how to to place objects in one particular area every time but now I would like the code for placing actors randomly so that they spawn any where. Thanks for your help guys.
My Snake Trapper game has some helper superclasses for your subclasses of World and Actor named XWorld and XActor (short for eXtended World and eXtended Actor). The XWorld class has this method:
1
2
3
4
5
6
7
8
9
/**
 * This method adds the given Actor object into the world at a random location.
 *
 * @param actor: the Actor object to add to the world
 */
public void addObject(Actor actor)
{
    addObject(actor, random(getWidth()), random(getHeight()));
}
'random' is a class field with a java.util.Random object:
1
public static java.util.Random = new java.util.Random();
I will update the scenario containing those classes to run in HTML5 and include the source.
danpost danpost

2018/2/7

#
Heet_Patel wrote...
is there a online version of green foot where you can create a game on its website or do you have to download it on every computer you have.
Maybe you can use the library computer to download the USB version of the application onto a memory stick.
Heet_Patel Heet_Patel

2018/2/7

#
danpost wrote...
Heet_Patel wrote...
Hi, I want a code which can help me place actors randomly when I press run. I have the code for how to to place objects in one particular area every time but now I would like the code for placing actors randomly so that they spawn any where. Thanks for your help guys.
My Snake Trapper game has some helper superclasses for your subclasses of World and Actor named XWorld and XActor (short for eXtended World and eXtended Actor). The XWorld class has this method:
1
2
3
4
5
6
7
8
9
/**
 * This method adds the given Actor object into the world at a random location.
 *
 * @param actor: the Actor object to add to the world
 */
public void addObject(Actor actor)
{
    addObject(actor, random(getWidth()), random(getHeight()));
}
'random' is a class field with a java.util.Random object:
1
public static java.util.Random = new java.util.Random();
I will update the scenario containing those classes to run in HTML5 and include the source.
So where do I write this code. In the World editor or the actor editor
Heet_Patel Heet_Patel

2018/2/7

#
danpost wrote...
Heet_Patel wrote...
is there a online version of green foot where you can create a game on its website or do you have to download it on every computer you have.
Maybe you can use the library computer to download the USB version of the application onto a memory stick.
Thanks Never thought of that.
danpost danpost

2018/2/7

#
You cannot call 'addObject' from an Actor class without first referencing a world to add the object into. Also, there is this:
danpost wrote...
The XWorld class has this method: << Code Omitted >>
which should be a big hint.
You need to login to post a reply.