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

2017/4/25

Randomly spawning actors

Nozarashi Nozarashi

2017/4/25

#
Hello, I have made it so that every time I reset the world, actors will randomly spawn. Everything is working fine. But I want the actors to respawn randomly when they have been eaten. If it helps, this is the code:
    for(int i = 0; i < 15; i++) 
    {
        int x = Greenfoot.getRandomNumber(getWidth() - 1);
        int y  = Greenfoot.getRandomNumber(getHeight() - 1);
        addObject(new Ant(), x, y);
    }
danpost danpost

2017/4/25

#
The code given is the code that initially places ants in the world. It has nothing to do with ants being eaten. Where is that code and your attempt to re-spawn the ants?
Nozarashi Nozarashi

2017/4/25

#
danpost wrote...
The code given is the code that initially places ants in the world. It has nothing to do with ants being eaten. Where is that code and your attempt to re-spawn the ants?
That's what I'm looking for, I don't know how to respawn them.
Super_Hippo Super_Hippo

2017/4/25

#
You can just a new one whenever one was eaten using the same code as above. You have to use 'getWorld().' before using world methods. An alternative would be to have a method which adds ants:
public void addAnts(int numberOfAnts)
{
    for (int i=0; i<numberOfAnts; i++) 
    {
        addObject(new Ant(), Greenfoot.getRandomNumber(getWidth()-1), Greenfoot.getRandomNumber(getHeight()-1));
    }
}
Then, your code above would be
addAnts(15);
and to add one ant when one was eaten
//when ant was removed
((MyWorld) getWorld()).addAnts(1); //replace MyWorld with the name of your world subclass
danpost danpost

2017/4/25

#
An easy way is to not remove and replace eaten ants at all -- just use 'setLocation' to randomly move them, reusing the ant that was already in the world.
Nozarashi Nozarashi

2017/4/25

#
danpost wrote...
An easy way is to not remove and replace eaten ants at all -- just use 'setLocation' to randomly move them, reusing the ant that was already in the world.
This is the code for the spider to eat an ant.
    Actor Ant;
    Ant = getOneObjectAtOffset(0,0,Ant.class);
    if (Ant !=null)
    { 
        World world;
        world = getWorld();
        world.removeObject(Ant);
Am I supposed to change removeObject to setLocation?
Super_Hippo Super_Hippo

2017/4/25

#
You can try this:
Actor ant = getOneIntersectingObject(Ant.class); //you can use the at offset method if you prefer it
if (ant != null)
{
    ant.setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()-1), Greenfoot.getRandomNumber(getWorld().getHeight()-1));
}
Note that this does not prevent the ant from "spawning" at the same position as it was, so instantly getting eaten again.
Nozarashi Nozarashi

2017/4/25

#
Super_Hippo wrote...
You can try this:
Actor ant = getOneIntersectingObject(Ant.class); //you can use the at offset method if you prefer it
if (ant != null)
{
    ant.setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()-1), Greenfoot.getRandomNumber(getWorld().getHeight()-1));
}
Note that this does not prevent the ant from "spawning" at the same position as it was, so instantly getting eaten again.
    Actor Ant;
    Ant = getOneObjectAtOffset(0,0,Ant.class);
    if (Ant !=null)
    { 
        World world;
        world = getWorld();
        world.removeObject(Ant); 
        Ant.setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()-1), Greenfoot.getRandomNumber(getWorld().getHeight()-1));
So is this correct or am I misunderstanding something.
Super_Hippo Super_Hippo

2017/4/25

#
No, it is just what I posted, no removing of the ant.
Nozarashi Nozarashi

2017/4/26

#
Super_Hippo wrote...
No, it is just what I posted, no removing of the ant.
I understand now. Cheers.
You need to login to post a reply.