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

2021/2/1

Randomly Generating Actors

JustAnArchosaur JustAnArchosaur

2021/2/1

#
So I'm new here and I want to know how to have an actor randomly spawn whenever another actor is killed, eaten, etc
danpost danpost

2021/2/1

#
JustAnArchosaur wrote...
So I'm new here and I want to know how to have an actor randomly spawn whenever another actor is killed, eaten, etc
One can randomize spawning location, which actor type to spawn and when to spawn (and possibly more things could be randomized). What combination of these are you wanting?
JustAnArchosaur JustAnArchosaur

2021/2/1

#
when to spawn and randomizing location of spawn
JustAnArchosaur JustAnArchosaur

2021/2/1

#
More specifically, my main character will "eat" another actor, then quickly afterwards, another of the same actor randomly spawning somewhere in the world
danpost danpost

2021/2/1

#
JustAnArchosaur wrote...
More specifically, my main character will "eat" another actor, then quickly afterwards, another of the same actor randomly spawning somewhere in the world
So, in world act method:
1
2
3
4
5
6
if (getObjects(OtherActor.class).isEmpty() && Greenfoot.getRandomNumber(150) == 0)
{
    int x = Greenfoot.getRandomNumber(getWidth());
    int y = Greenfoot.getRandomNumber(getHeight());
    addObject(new OtherActor(), x, y);
}
JustAnArchosaur JustAnArchosaur

2021/2/2

#
Thank you for your help :)
You need to login to post a reply.