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

2017/5/17

Falling objects

Brettwolin Brettwolin

2017/5/17

#
How do I make an object disappear when it hits the bottom of the screen. And how do I make new objects randomly fall from the top.
Yehuda Yehuda

2017/5/17

#
To make an object disappear when it hits the bottom of the screen add the following at the bottom of the Actor's act method:
if (getY() > getWorld().getHeight() - getImage().getHeight() / 2) {
    getWorld().removeObject(this);
}
To make objects randomly get added to the top of thw World then put the following into the World's act method:
if (Greenfoot.getRandomNumber(100) < 4) { // percent chance that an actor will be added, now it's a 4 % chance
    ActorName actor = new ActorName();
    addObject(actor, Greenfoot.getRandomNumber(getWidth()), actor.getImage().getHeight() / 2);
}
You need to login to post a reply.