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

2013/11/25

How to sporn random

1
2
3
Asoraso Asoraso

2013/11/25

#
hey guys i´m new here. Can anyone tell me how to sporn an actor on a random place? thx for anser me
Gevater_Tod4711 Gevater_Tod4711

2013/11/25

#
To spawn an actor you need to use the addObject method. To generate a random number you can use Greenfoot.getRandomNumber(int). So of you want an object to spawn anywhere in your world you can use this code:
addObject(new YourClassName(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
Asoraso Asoraso

2013/11/25

#
sorry for asking but here do i have to past the code ?
Gevater_Tod4711 Gevater_Tod4711

2013/11/25

#
You need to add this code somewhere in your world class. I think in the act method would be best. (With an if-condition because otherwhise there will be a new object every act). But you can also past this code in an actor subclass (also in the act method). But then you have to change it like this:
getWorld().addObject(new YourClassName(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));  
Asoraso Asoraso

2013/11/25

#
and if the actor should sporn random in the world what should i change in the code ??? because if i do the if thing there isn´t any thing that spowned
Asoraso Asoraso

2013/11/25

#
the code : public void act() { if (!getWorld().getObjectsAt(getX() -1, getY() -1, Walter.class).isEmpty()) { getWorld().addObject(new Walter(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); }}
Gevater_Tod4711 Gevater_Tod4711

2013/11/25

#
I think you should use a different if clause. This if clause means that there should only spawn new objects if there is already an object at the coordinate x-1, y-1 of your actor. When do you want a new actor to spawn in the world? E.g. you could spawn a new actor if there are no more in the world:
public void act() {
    if (getWorld().getObjects(Walter.class).isEmpty()) {
        getWorld().addObject(new Walter(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
    }
}
Asoraso Asoraso

2013/11/25

#
i want a new actor sporn if i click act ore run so in the end it should sporn anywhere on the map and after 10 second it should spown at an ather place! but i thought i can do the time thing after the spown thing is ready
Gevater_Tod4711 Gevater_Tod4711

2013/11/25

#
To just spawn new objects randomly you can use the code I gave you before without the if clause. To let the actors spawn after a certain time you need to add a counter. E.g. like this:
private int counter = 0;

public void act() {
    counter++;
    if (counter > 500) { //this value depends on your scenario speed; I'm not shure which value is about 10 seconds; You'll have to try;
        getWorld().addObject(new Walter(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
        counter = 0;
    }
}
Asoraso Asoraso

2013/11/25

#
and the counter get place in the main klass actor or in the clas roboter ? (the subclass of actor and overclass of walter)
Asoraso Asoraso

2013/11/25

#
and the line done at the buttom said "method act() is already defined in class walter what schould i do ? i taked that code from your message but they get more and more but the first dont disapper so it is a 2. problem i have code : addObject(new YourClassName(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
danpost danpost

2013/11/25

#
The following is if you only have one Walter object in the world at a time:
//in WORLD class
// in 'act' method
if (getObjects(Walter.class).isEmpty()) spawnWalter();
// new method
private void spawnWalter()
{
    int x = Greenfoot.getRandomNumber(getWidth());
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(new Walter(), x, y);
}
// in WALTER class
// instance field
private int counter;
// in 'act' method
runCounter();
// new method
private void runCounter()
{
    counter++;
    if (counter == 500) getWorld().removeObject(this);
}
// in MAIN actor class
Walter walter = getOneIntersectingObject(Walter.class);
if (walter != null) getWorld().removeObject(walter);
Asoraso Asoraso

2013/11/25

#
thx but in line 22 need it be in a public void ore like 12 in no .... (i don´t know the word)
Asoraso Asoraso

2013/11/25

#
in a public void the line 23 the "(Waltter.class)" is red and the line down said incompatible types
danpost danpost

2013/11/25

#
The code for the MAIN actor class (anything beyond line 22) should be in the 'act' method or a method the 'act' method calls.
There are more replies on the next page.
1
2
3