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

2020/6/24

Error when removing Object

DenisHtl DenisHtl

2020/6/24

#
public void act() 
    {  
       
        if(Greenfoot.isKeyDown("r"))
        {
            turn();
            move(30);
            Actor enemy = getOneObjectAtOffset(0,0,Enemy.class);
         if(enemy != null)
         {
             World detect;
             detect = getWorld();
             detect.removeObject(enemy);
             
         }
        }

    }   
    public void addObjects(){
        Player player = (Player)getWorld().getObjects(Player.class).get(0);
        getWorld().addObject(new shoot(), player.getX(), player.getY());
    }
    public void turn(){
        Enemy enemy = (Enemy)getWorld().getObjects(Enemy.class).get(0);
        turnTowards(enemy.getX(), enemy.getY()); 

    }
Super_Hippo Super_Hippo

2020/6/24

#
The error probably happens because you try to turn to the first enemy in the list (turn method) without checking if there is an object in the list at all. As long as you have one, it is fine. When it is removed, the list is empty and you try to get the location of nothing.
DenisHtl DenisHtl

2020/6/24

#
Super_Hippo wrote...
The error probably happens because you try to turn to the first enemy in the list (turn method) without checking if there is an object in the list at all. As long as you have one, it is fine. When it is removed, the list is empty and you try to get the location of nothing.
thx i actualy know that i just dont know how to fix
Super_Hippo Super_Hippo

2020/6/24

#
public void turn()
{
    Enemy enemy = (Enemy) getWorld().getObjects(Enemy.class).get(0);
    if (enemy != null)
    {
        turnTowards(enemy.getX(), enemy.getY());
    }
}
As a side note, it might be a good idea to rename the method to something else because you are overwriting the Actor’s turn method.
DenisHtl DenisHtl

2020/6/24

#
Super_Hippo wrote...
public void turn()
{
    Enemy enemy = (Enemy) getWorld().getObjects(Enemy.class).get(0);
    if (enemy != null)
    {
        turnTowards(enemy.getX(), enemy.getY());
    }
}
oh yeah right sorry im new to all this but do you have a answer to my first problem As a side note, it might be a good idea to rename the method to something else because you are overwriting the Actor’s turn method.
Super_Hippo Super_Hippo

2020/6/24

#
If that didn’t solve the problem, give more detail about what exactly the problem is.
Roshan123 Roshan123

2020/6/25

#
public void act() 
    {  
       
        if(Greenfoot.isKeyDown("r"))
        {
            turn();
            move(30);
            Actor enemy;
enemy=getOneObjectAtOffset(0,0,Enemy.class);
         if(enemy != null)
         {
             World detect;
             detect = getWorld();
             detect.removeObject(enemy);
             
         }
        }

    }   
   
try this if this doesnt work then try this
Roshan123 Roshan123

2020/6/25

#
if(getWorld()!=null)
{
   getWorld.removeObject(this);
}
danpost danpost

2020/6/25

#
Super_Hippo wrote...
<< Code Omitted >> As a side note, it might be a good idea to rename the method to something else because you are overwriting the Actor’s turn method.
There was NO overriding of any method of Actor here. To override turn(int) of Actor, an int parameter would be required.
Super_Hippo Super_Hippo

2020/6/25

#
True. It can still be confusing to use the same name.
danpost danpost

2020/6/25

#
Super_Hippo wrote...
True. It can still be confusing to use the same name.
I presumed you knew that, but just overlooked it. I only stated that for Roshan123's benefit.
You need to login to post a reply.