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

2013/4/16

Stopping the scenario.....

1
2
jennnnay24 jennnnay24

2013/4/16

#
If anyone could help me out with this, I would greatly appreciate it. I need to figure out how to stop the scenario when the number of wombats reaches 3 OR the number of leaves reaches 3. I'm new to this so I don't know if you would need me to post the code I have. Let me know.
JetLennit JetLennit

2013/4/16

#
1st add the ints w and l then every time a wombat spawns add 1 to w and every time a leaf spawns add to l then add this in the world
 
public void act()
{
if(w == 3 || l == 3) Greenfoot.stop();
}
jennnnay24 jennnnay24

2013/4/16

#
I'm a beginner at this....How do you add the ints w and l?
JetLennit JetLennit

2013/4/16

#
sorry, i forgot that.... add this right after the public class (your world name) extends World {
public int w;
public int l;
and in the method that has the name of your world in it add
 w = 0;
l = 0;
jennnnay24 jennnnay24

2013/4/16

#
I'm confused. This is the code I have right now for the world. public class WombatWorld extends World { /** * Create a new world with 8x8 cells and * with a cell size of 60x60 pixels */ public WombatWorld() { super(8, 8, 60); setBackground("cell.jpg"); setPaintOrder(Wombat.class, Leaf.class); populateWorld(); } /** * Populate the world with a fixed scenario of wombats and leaves. */ public void populate() { Wombat w1 = new Wombat(); addObject(w1, 3, 3); Wombat w2 = new Wombat(); addObject(w2, 1, 7); Leaf l1 = new Leaf(); addObject(l1, 5, 3); Leaf l2 = new Leaf(); addObject(l2, 0, 2); Leaf l3 = new Leaf(); addObject(l3, 7, 5); Leaf l4 = new Leaf(); addObject(l4, 2, 6); Leaf l5 = new Leaf(); addObject(l5, 5, 0); Leaf l6 = new Leaf(); addObject(l6, 4, 7); } /** * Place a number of leaves into the world at random places. * The number of leaves can be specified. */ public void randomLeaves(int howMany) { for(int i=0; i<howMany; i++) { Leaf leaf = new Leaf(); int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(leaf, x, y); } } public void populateWorld() { int i = 0; for (i=0; i<10; i++) addObject (new Wombat(), Greenfoot.getRandomNumber (8), Greenfoot.getRandomNumber (8)); for (i=0; i<30; i++) addObject (new Leaf(), Greenfoot.getRandomNumber (8), Greenfoot.getRandomNumber (8)); } }
JetLennit JetLennit

2013/4/16

#
try changing that world code with this world code (this should work)
public class WombatWorld extends World
{ 
    public int w;
    public int l;
    /**
     * Create a new world with 8x8 cells and
     * with a cell size of 60x60 pixels
     */
     public void act()
     {
        if(w == 3 || l == 3) Greenfoot.stop();
     }
    public WombatWorld() 
    {
        w = 0;
        l = 0;
        super(8, 8, 60);        
        setBackground("cell.jpg");
        setPaintOrder(Wombat.class, Leaf.class);
        populateWorld();
    }
    /**
     * Populate the world with a fixed scenario of wombats and leaves.
     */    
    public void populate()
    {
        Wombat w1 = new Wombat();
        addObject(w1, 3, 3);
        
        Wombat w2 = new Wombat();
        addObject(w2, 1, 7);

        Leaf l1 = new Leaf();
        addObject(l1, 5, 3);

        Leaf l2 = new Leaf();
        addObject(l2, 0, 2);

        Leaf l3 = new Leaf();
        addObject(l3, 7, 5);

        Leaf l4 = new Leaf();
        addObject(l4, 2, 6);

        Leaf l5 = new Leaf();
        addObject(l5, 5, 0);
        
        Leaf l6 = new Leaf();
        addObject(l6, 4, 7);
    }
    
    /**
     * Place a number of leaves into the world at random places.
     * The number of leaves can be specified.
     */
    public void randomLeaves(int howMany)
    {
        for(int i=0; i<howMany; i++) {
            Leaf leaf = new Leaf();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(leaf, x, y);
        }
    }

    
    public void populateWorld()
    {
        int i = 0;
        
        for (i=0; i<10; i++)
{
            addObject (new Wombat(), Greenfoot.getRandomNumber (8), Greenfoot.getRandomNumber (8)); 
            w++;
}
            
            
        for (i=0; i<30; i++)
{
            addObject (new Leaf(), Greenfoot.getRandomNumber (8), Greenfoot.getRandomNumber (8)); 
            l++;
    }
}
}
JetLennit JetLennit

2013/4/16

#
click view plain and copy that and paste it in your class
JetLennit JetLennit

2013/4/16

#
if you are new.... how did you write that?
jennnnay24 jennnnay24

2013/4/16

#
It says "cannot find symbol - constructor World ()" and line 14 is highlighted
jennnnay24 jennnnay24

2013/4/16

#
It's given to us as part of an assignment, we just have to add things like the loops at the bottom, I had to add those. This is the only thing I am having trouble with.
JetLennit JetLennit

2013/4/16

#
wat if you cchange lines 9-21 to
    public WombatWorld() 
    {
        w = 0;
        l = 0;
        super(8, 8, 60);        
        setBackground("cell.jpg");
        setPaintOrder(Wombat.class, Leaf.class);
        populateWorld();
    }
public void act()
     {
        if(w == 3 || l == 3) Greenfoot.stop();
     }
danpost danpost

2013/4/16

#
You do not need the 'w' or the 'l' to accomplish this. Just use this for your 'act' method:
public void act()
{
    if (getObjects(Wombat.class).size() == 3 || 
        getObjects(Leaf.class).size() == 3)
            Greenfoot.stop();
}
Using the variables just complicates matters.
JetLennit JetLennit

2013/4/16

#
I did not know that was possible....
jennnnay24 jennnnay24

2013/4/16

#
use that in the world? or in the classes?
MatheMagician MatheMagician

2013/4/16

#
In the act method of the world class.
There are more replies on the next page.
1
2