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

2019/11/11

Selecting only one spacecraft

Finn.C Finn.C

2019/11/11

#
Hi! I was just trying to make an space invaider type of game and was wondering how I can only select one thing out of a bunch. For Example: I have 10 spacecrafts and only want one to shoot. Is there a way to do that? Thanks!
danpost danpost

2019/11/11

#
Finn.C wrote...
Hi! I was just trying to make an space invaider type of game and was wondering how I can only select one thing out of a bunch. For Example: I have 10 spacecrafts and only want one to shoot. Is there a way to do that? Thanks!
Random? From world?
java.util.List crafts = getObjects(Spacecraft.class);
if ( ! crafts.isEmpty() ) ((Spacecraft) crafts.get(Greenfoot.getRandomNumber(crafts.size()))).shoot();
You need to login to post a reply.