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

2022/1/21

How to add a random object while the game is running

Nufaralbu Nufaralbu

2022/1/21

#
I created a game where, after its starts, the bug eats the leaf. But I want to add a new leaf after one has been eaten, in another random place. I tried the addObject function, but it didn't work.
danpost danpost

2022/1/21

#
Nufaralbu wrote...
I created a game where, after its starts, the bug eats the leaf. But I want to add a new leaf after one has been eaten, in another random place. I tried the addObject function, but it didn't work.
There are a couple (at least) ways to accomplish that. You could use an act method in your world class to maintain a certain number of leaves in the world. Or, you could just move the leafs to new random locations when "eaten".
Nufaralbu Nufaralbu

2022/1/21

#
And how do I move the leaf to a new location? Is there a function?
danpost danpost

2022/1/21

#
Nufaralbu wrote...
And how do I move the leaf to a new location? Is there a function?
Get a reference to the eaten leaf. Next, get a random x and a random y. Then, set a new location (x, y) for the referenced leaf.
You need to login to post a reply.