I'm looking for a way for the world (or possibly an actor) to recognize how many objects are in it. Then perform an if statement when there are different number of objects in the world.
Help or suggestions are greatly appreciated,
Thanks


1 | int actors = getObjects( null ).length(); |
1 2 3 4 5 6 7 8 | public void checkNextLevel() { int actors = getObjects( null ).length( 1 ); if (actors == 1 ) { Greenfoot.setWorld(beach2); } } |
1 2 3 4 5 6 7 8 | public void checkNextLevel() { int actors = getObjects( null ).length(); if (actors == 1 ) { Greenfoot.setWorld(beach2); } } |
1 2 3 4 5 6 7 8 | public void checkNextLevel() { int actors = getWorld().getObjects( null ).length(); if (actors == 1 ) { Greenfoot.setWorld(beach2); } } |
1 2 3 4 5 6 7 8 | public void checkNextLevel() { int actors = numberOfObjects(); if (actors == 1 ) { Greenfoot.setWorld(beach2); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class beach1 here. * * @author (Joe Schmoe) * @version (v3) */ public class beach1 extends World { /** * Constructor for objects of class beach1. * */ public beach1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); prepare(); checkNextLevel(); } /** * Check if we should move to the next level, beach2 */ public void checkNextLevel() { int actors = numberOfObjects(); if (actors <= 1 ) { Greenfoot.setWorld( new beach2()); } } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { fish fish = new fish(); addObject(fish, 319 , 83 ); starfish starfish = new starfish(); addObject(starfish, 391 , 80 ); starfish starfish2 = new starfish(); addObject(starfish2, 451 , 119 ); seal seal = new seal(); addObject(seal, 336 , 239 ); seal.setLocation( 249 , 229 ); } } |