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

2013/2/16

What am I doing wrong with this code?

Gingervitis Gingervitis

2013/2/16

#
When the cage eats 15 Blaster Bullets, it will remove itself. That works fine. When two cages are removed, I want it to create a new object, but nothing happens. Here is my code
public void tryToEat()
    {
        Actor bb1 = getOneIntersectingObject(BlasterBullet.class);
        if (bb1 !=null)
        {
            getWorld().removeObject(bb1);
            eat(BlasterBullet.class);
            bb1Eaten++;
            if (bb1Eaten == 15)
            {
                removeObject();
                cagesDestroyed++;

            }
            if (cagesDestroyed == 2)
            {
                createNewTurtleBlaster();
            }            
        } 
    }
Gevater_Tod4711 Gevater_Tod4711

2013/2/16

#
It would be helpfull to know the code of the methods removeObject() and createNewTurtleBlaster()
Gingervitis Gingervitis

2013/2/16

#
Oops. Sorry, I didn't think that would be needed to solve the problem. Here is my entire cage class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cage here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cage extends Animal
{
    private int bb1Eaten;
    private int cagesDestroyed;
    /**
     * Act - do whatever the Cage wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        tryToEat();
    }

    public void tryToEat()
    {
        Actor bb1 = getOneIntersectingObject(BlasterBullet.class);
        if (bb1 !=null)
        {
            getWorld().removeObject(bb1);
            eat(BlasterBullet.class);
            bb1Eaten++;
            if (bb1Eaten == 15)
            {
                cagesDestroyed++;
                removeObject();
                
                if (cagesDestroyed == 2)
                {
                    createNewTurtleBlaster();
                }
            }
        } 
    }

    public void createNewTurtleBlaster()
    {
        TurtleBlaster tb;
        tb = new TurtleBlaster();
        World world;
        world = getWorld();
        world.addObject(tb,300,10);
    }

    public void removeObject()
    {
        World world;
        world = getWorld();
        world.removeObject(this);
    }

}
Gevater_Tod4711 Gevater_Tod4711

2013/2/16

#
The problem is that the int value cagesDestroyed is counted up one time and then the object is deleted what causes that the value is deleted too. So the value will never be 2. If you declare the variable as static it will count up for every object that is destroyed and then it should work. So you just have to change line 12 to private static int cagesDestroyed;
Gingervitis Gingervitis

2013/2/16

#
Thank you
You need to login to post a reply.