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

2013/6/9

way to make world recognize how many objects are in it?

1
2
joeschmoe joeschmoe

2013/6/9

#
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
erdelf erdelf

2013/6/9

#
1
int actors = getObjects(null).length();
this should give you the number of objects in the world
joeschmoe joeschmoe

2013/6/9

#
Thank you!
joeschmoe joeschmoe

2013/6/9

#
I think I most likely made an obvious mistake but I have this and am getting an error:
1
2
3
4
5
6
7
8
public void checkNextLevel()
    {
        int actors = getObjects(null).length(1);
        if (actors == 1)
        {
            Greenfoot.setWorld(beach2);
        }
    }
and I'm getting this: "cannot find symbol - method length(int) " I'm still learning, this is only my first program...
joeschmoe joeschmoe

2013/6/9

#
I guess I kinda understand what that phrase is but not how to use it :P
erdelf erdelf

2013/6/9

#
why are you writing a 1 in my line?
joeschmoe joeschmoe

2013/6/9

#
I tried doing a few things to the if statement and nothing worked, kept getting "cannot find symbol - method length(int) " So I thought put a number there or something? I'm not sure, I am ignorant.... I want if there is 1 object in the world to run Greenfoot.setWorld(beach2)
erdelf erdelf

2013/6/9

#
I meant the line I wrote for you, why is there a 1, i didnt wrote that
joeschmoe joeschmoe

2013/6/9

#
I put it there because I was confused.
Zamoht Zamoht

2013/6/9

#
1
2
3
4
5
6
7
8
public void checkNextLevel() 
    
        int actors = getObjects(null).length(); 
        if (actors == 1
        
            Greenfoot.setWorld(beach2); 
        
    }
If you want to use this from an actor the code would look like this:
1
2
3
4
5
6
7
8
public void checkNextLevel() 
    
        int actors = getWorld().getObjects(null).length(); 
        if (actors == 1
        
            Greenfoot.setWorld(beach2); 
        
    }
joeschmoe joeschmoe

2013/6/9

#
Niether of those worked, the first gave me the same error as before and the second (after putting it into an Actor) gave me the same arror again... Do I have to do something to the length() ? Now I'm trying this
1
2
3
4
5
6
7
8
public void checkNextLevel()   
    {   
        int actors = numberOfObjects();   
        if (actors == 1)   
        {   
            Greenfoot.setWorld(beach2);   
        }   
    }
but the error says, "cannot find symbol - variable beach2" I have tried replace (beach2) with (World beach2), (World "beach2.class"), ("beach2.class") When I do something with 2 words it expects a ")" after the first....
danpost danpost

2013/6/9

#
You need to create a new beach2 object. You can use 'new beach2()' .
joeschmoe joeschmoe

2013/6/9

#
Thanks, I put that in, compiled, and got no errors. But when I run the program and there is 1 Actor on the screen the world does not switch... What else am I missing?
erdelf erdelf

2013/6/9

#
are you calling this method in the act method?
joeschmoe joeschmoe

2013/6/9

#
There is no act method, it's in a world class. The full code for the file is
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);
    }
     
    }  
There are more replies on the next page.
1
2