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

2015/1/11

trying to make bullet dissapear when it hits enemy but cannot find symbol variable getWorld

1
2
khalid11 khalid11

2015/1/11

#
inside my bullet class
public void destroyEnemies()
    {
        
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(enemy != null) 
        {
           World myWorld = getWorld();
           getWorld().removeObject(enemy);  
           getWorld.removeObject(this);                   
        }
    }
//cannot find symbol variable getWorld
erdelf erdelf

2015/1/11

#
line 9, you forgot the braces
getWorld().removeObject(this);               
khalid11 khalid11

2015/1/11

#
thank you let me try
khalid11 khalid11

2015/1/11

#
nice one it worked
khalid11 khalid11

2015/1/11

#
A new error appears in a new window
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.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:681)
	at greenfoot.Actor.getOneIntersectingObject(Actor.java:930)
	at bullet.kill(bullet.java:60)
	at bullet.act(bullet.java:30)
erdelf erdelf

2015/1/11

#
I need to see the kill method for this one
khalid11 khalid11

2015/1/11

#
from which class ,my bullets?
erdelf erdelf

2015/1/11

#
yeah
khalid11 khalid11

2015/1/11

#
its just this 
    public void destroyEnemies()
    {
        
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(enemy != null) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(enemy);  
            Level1 level1 = (Level1)myWorld;
            Counter counter = level1.getCounter();
            counter.addScore();
        }
    }
danpost danpost

2015/1/11

#
Actually, it is your act method that you need to show (yes, of your bullet class).
khalid11 khalid11

2015/1/11

#
 public void act() 
    {
        destroyEnemies ();
        move(5);
        kill();
        if (this.atWorldEdge()) {
            getWorld().removeObject(this);
        }     
    }
danpost danpost

2015/1/11

#
Ok. Apparently you are removing the objects from the world in the kill method; and then, after that in the kill method you are trying to use the 'getOneIntersectingObject' method which cannot work if the actor was removed from the world. If I am mistaken, which I do not think I am, go ahead and post the code of the kill method. Once you get by that, you will get the same error in the 'atWorldEdge' method which will then need to be dealt with.
khalid11 khalid11

2015/1/11

#
public void kill() { //"Enemy" can be any class that you want the bullet to destroy. Actor blueenemy = getOneIntersectingObject(blueenemy.class); if(blueenemy != null) { getWorld().removeObject(blueenemy); } }
danpost danpost

2015/1/11

#
Ok. Clear your terminal window; compile your project; re-create the error; and copy/paste the entire error message along the entire bullet class code (just select all and copy/paste). It is difficult to figure out what is going on when only getting bits and pieces at different times.
khalid11 khalid11

2015/1/11

#
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.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:681)
	at greenfoot.Actor.getOneIntersectingObject(Actor.java:930)
	at bullet.kill(bullet.java:62)
	at bullet.act(bullet.java:31)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
There are more replies on the next page.
1
2