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

2014/3/4

Count after Rocket and Target have been removed

Michi2610 Michi2610

2014/3/4

#
Hi Guys, i´m really new with programming and i want to write a little Tank programm. If a rocket which was fired from a tank hits a target i want them both to be removed. And after i hit all targets, 5 in my case, the world should change.
public class Targets extends Exercises
{
    int counter = 0;
    /**
     * Act - do whatever the Ziele wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        hit();

    }

    private void hit()
    {
        Actor rocket = getOneIntersectingObject(Rocket.class);  
        if (rocket != null) 
        {  
            getWorld().removeObject(rocket);  
            getWorld().removeObject(this);
            counter++;

        }  
    }
}
The problem is that my programm stucks and i get an error if my rocket dont hits a target and is beeing deleted by my "atWorldEdge" method also i dont know why the counter doesnt get higher... The error message is
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.

and

java.lang.NullPointerException

Please help me :(
danpost danpost

2014/3/4

#
The problem code is in the class of the rocket. Please post the code for that class.
Michi2610 Michi2610

2014/3/4

#
Code of the rocket;
public class Rocket extends Weapons
{
    int csmoke = 2;

    /**
     * Act - do whatever the rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        move(3.0);
        edge(); // if at World Edge, remove Rocket
        addSmoke();

    }

    public void addSmoke()
    {
        Smoke smoke = new Smoke();
        csmoke--;
        if(csmoke==0 )
        {
            getWorld().addObject(smoke, getX(), getY());
            smoke.move(-10);
            csmoke = 2;

        }
    }
    
        public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    
    /**
     * If atworldedge remove this Object
     */   
    public void edge()
    {
        if(atWorldEdge())
        {
            getWorld().removeObject(this);
        }

    }
danpost danpost

2014/3/4

#
You cannot add a smoke object into a world that your actor is not in. When your 'edge' method removes the rocket from the world, 'getWorld' will then return 'null'. By calling 'addSmoke' after 'edge' you end up trying to add smoke into a 'null' world (impossible, hence your error). Just reverse the calls so that 'edge' is checked after you 'addSmoke'.
Michi2610 Michi2610

2014/3/4

#
ok thanks this works now but how can i count how much targets i have hit? if i check the int of the other targets which normally should be 1 after a target has been destroyed it is still 0 :s i want to destroy 5 targets and then get to the next level.
danpost danpost

2014/3/4

#
Each target you create gets its own field for 'count'. Once you remove the target from the world and lose all references to it, it and its fields are flagged for garbage collection. You either need to move the field to your subclass of world (which also is an object that if you switch worlds and lose all references to the original one, the same thing will occur), or you can make the field a 'static' field whose value will need to be reset anytime a new world is created.
You need to login to post a reply.