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

2013/4/18

Bullet disapearing cause errors

1
2
mufusion mufusion

2013/4/18

#
when the bullet hit the enemy it decrese its health by 1 and disapear but after the first disapeared bullet i get this error: 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:663) at greenfoot.Actor.getX(Actor.java:157) at Bullet.act(Bullet.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) it says that the actor bullet is not there what is true. because i wanted that the bullet disapear heres the code for the Bullet
 private boolean atWorldEdge()  
    {  
        return (getX()==0 ||  
                getY()==0 ||  
                getX()==getWorld().getWidth()-1 ||  
                getY()==getWorld().getHeight()-1);  
                
    }  
 
    
    public void act() 
    {
        
      hitEnemy();
    {
        
        
       setLocation(getX() + 36, getY());
      
           if(atWorldEdge())  
    {  
        getWorld().removeObject(this);  
        return;  
    }  
    }
   
}  
 public void hitEnemy() {  
    gegner1 enemy = (gegner1) getOneObjectAtOffset(0, 0, gegner1.class);  
    if (enemy != null) {  
        enemy.setHealth(-1);
        getWorld().removeObject(this);
    }  
    if (enemy == null) {
        getWorld().removeObject(enemy);
when i delete getWorld().removeObject(this); everything is ok but i want the Bullet to disapear after hitting the enemy
davmac davmac

2013/4/18

#
Please fix your indentation... (use 'auto-layout' in the editor).
danpost danpost

2013/4/18

#
I think you know what the problem is and just do not know what to do to remedy it. After an enemy is hit, you get an error because you are trying to move the actor in a world that it was removed from. The fix is to not execute the rest of the lines of code in the act method if the object is not in a world (after hitting an enemy); or, to only execute the rest of the method if the object is still in the world.
// put the following line after 'hitEnemy();'
if (getWorld() != null)
Line 23 in your code above is not needed since the method will be exited anyways (as there is no more code to execute there). Also, lines 34 and 35 are basically meaningless (if there is no enemy, remove him?). Those lines can be removed, also.
mufusion mufusion

2013/4/18

#
line 34 and 35 are deletet anyway, after posting this i saw that their useless. im in this moment not able to edit it but i will edit it tomorrow at school ( i live in the time zone GMT +2) after spawning some more enemys i get an error that says something like outofmemory does it mean the RAM ? is the computer not good enough?
danpost danpost

2013/4/18

#
Probably something in your code. Possibly creating an abundance of images or actors via the act method (instead of the constructor).
mufusion mufusion

2013/4/19

#
the exactly error code is the following: java.lang.OutOfMemoryError: Java heap space Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException after i added the following in the gegner1 code the error startet, but when i remove it or set it to comment the following error apears multiple times : Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space i only added this :
public void spawn()
    {      

    if (counter == 10)  
    {  
    getWorld().addObject (new gegner1(),800,420);
    getWorld().addObject (new gegner1(),800,440);
    getWorld().addObject (new gegner1(),800,460);
    getWorld().addObject (new gegner1(),800,480);
    getWorld().addObject (new gegner1(),800,500);  
    counter = 0;  
    }  
    else  
    {  
    counter ++;

    }  

    }
     
by deleting it another error apears so i think its not the spawn that cause the errors
danpost danpost

2013/4/19

#
Maybe you should post the whole class and copy/paste the complete error messages so we can get a better idea of what is going on.
mufusion mufusion

2013/4/19

#
the two errors where the full error messages. the gegner1 code :
public class gegner1 extends Actor {  

    private int health = 10; 
    private int counter = 10;

    public void setHealth(int points) {  
        health += points;  
    }  

    public int getHealth() {  

        return health;  
    }  

    public void act() 
    {
        remove();
        if (getWorld() != null);
        getHealth();
        // spawn();
        moveToPlayer();
    }    

    public void moveToPlayer()  
    {    

        spieler player = (spieler) getWorld().getObjects(spieler.class).get(0);  
        if(getX() < player.getX())   
            setLocation(getX() + 1, getY());   
        if(getX() > player.getX())   
            setLocation(getX() - 1, getY());   
        if(getY() < player.getY())   
            setLocation(getX() +0,getY()+ 1);   
        if(getY() > player.getY())   
            setLocation(getX() +0, getY()- 1);   

    }  
    /**public void spawn()
    {      

    if (counter == 10)  
    {  
    getWorld().addObject (new gegner1(),800,420);
    getWorld().addObject (new gegner1(),800,440);
    getWorld().addObject (new gegner1(),800,460);
    getWorld().addObject (new gegner1(),800,480);
    getWorld().addObject (new gegner1(),800,500);  
    counter = 0;  
    }  
    else  
    {  
    counter ++;

    }  

    }
     */

    public void remove()
    {

        if (health == 0)
        {getWorld().removeObject(this);
        }
    }

}
i set the critical part to comment, the other error is fixed only the java.lang.OutOfMemoryError: Java heap space error was still there. i wanted to try it at home but i didnt get an error greenfoot just dont react anymore. on the other computer a window with error messages appeared
davmac davmac

2013/4/19

#
There is nothing in that code that would cause a problem, as far as I can see. However, you have two lines which do nothing - lines 18 and 19. Line 18 is an "if" statement followed immediately by a semicolon (';') which is pretty much always wrong. Line 19 calls a method to retrieve a value, but doesn't do anything with the value.
JetLennit JetLennit

2013/4/19

#
/edit]If the actor is not in the world when getX()/getY() is called It usually causes this error never mind
danpost danpost

2013/4/19

#
Line 18 should probably be
if (getWorld() == null) return;
mufusion mufusion

2013/4/19

#
line 18 and 19 and the other health stuff where taken from an old topic i found via google. the spawn method cause the problem but i dont know why
danpost danpost

2013/4/19

#
Well, I will tell you that '10' as the limit for the counter to spawn four enemies will produce quite a few of them in a very short time (approximately 24 per second on an average speed scenario). Maybe you should increase that number a bit. And maybe you should put a limit on the number of enemies in the world at one time.
mufusion mufusion

2013/4/19

#
what does 10 stand for? i dont think its 10 seconds. is it 10 milliseconds?
danpost danpost

2013/4/19

#
It is counting frames (or act cycles).
There are more replies on the next page.
1
2