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

2017/11/28

How do i make a flower respawn

JD.Knox JD.Knox

2017/11/28

#
I have recently started Greenfoot or coding for that matter and i could use some help. I wish to make it so that when the Bee runs over the flower it disappears and re-spawns some were else after a short delay (maybe 1-3 seconds) but i have no clue how to make them re-spawn. (I know how to make it disappear i have done that already.) To make the flower spawn in the first place i use the code: addObject(new Flower(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); (I use this multiple times to get multiple flowers.)
danpost danpost

2017/11/28

#
JD.Knox wrote...
To make the flower spawn in the first place i use the code: < Code Omitted > (I use this multiple times to get multiple flowers.)
There almost always more than one way to program a specific behavior. There are certainly multiple ways to accomplish what you are wanting. None of them are particular easy for a beginner in programming. You could use a list of timer integers in your world class and have the world run the timers and spawn a new flower once a timer has run its course (upon which the timer is removed from the list -- a timer is added to the list when a flower is eaten). Another way is to have each flower "respawn" itself -- instead of removing it from the world, you would just remove the image that is set to it and start a timer. To "respawn", just replace the original image and relocate it to a new random location in the world. Another way is to add a spawner actor at some random location in the world which runs the timer and adds a new flower at its location before removing itself from the world at the end of the delay. There are probably other ways as well; but one of these should suffice (the last way may be easiest for a novice programmer).
JD.Knox JD.Knox

2017/11/28

#
i don't need a timer that was optional
danpost danpost

2017/11/28

#
JD.Knox wrote...
i don't need a timer that was optional
Then just set a new random location for any eaten flower (no need to remove one from the world).
JD.Knox JD.Knox

2017/11/29

#
Thank you very much for the help. I ended up doing it a different way like this: Actor Flower; Flower = getOneObjectAtOffset(0,0,Flower.class); if(Flower!= null) { World world; world = getWorld(); world.removeObject(Flower); world.addObject(Flower,Greenfoot.getRandomNumber(1200),(Greenfoot.getRandomNumber(850))); } But thanx anyway. Figuring it out yourself is the best way to learn in my opinion!
Super_Hippo Super_Hippo

2017/11/29

#
Actor flower = getOneObjectAtOffset(0, 0, Flower.class);
if (flower != null)
{
    setLocation(flower, Greenfoot.getRandomNumber(1200), Greenfoot.getRandomNumber(850));
}
True, but it is quite complicated to remove the object and re-add it instead of simply change its location.
You need to login to post a reply.