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

2020/1/9

Actor not in world error when enemy defeated

marcelw123 marcelw123

2020/1/9

#
I am creating a space game where you need to shoot enemies. I made the player ship shoot and was trying to make the bullets disappear when they hit the edge of the world which i have managed to do and i also made it so that when the enemy is hit the bullet also disappears but the code for it seems to create an error when the enemy is hit. If i delete the DestroyEnemy() public void then the error disappears and the bullet makes the enemy disappear but the bullet itself just goes through the enemy instead of disappearing wh
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PlayerBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerBullet extends Actor
{
    /**
     * Act - do whatever the PlayerBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation(-90);
        move(4);

        DestroyEnemy();
        CheckEdge();

    } 

    public void CheckEdge()
    {
        if(isAtEdge())
        {                
            getWorld().removeObject(this);                
        }
    }

    public void DestroyEnemy()
    {
        if (isTouching(Enemy.class))
        {           
            getWorld().removeObject(this);             
        }
    }
}
en hitting the enemy, how can i fix this.
danpost danpost

2020/1/9

#
marcelw123 wrote...
the code (for it) seems to create an error when the enemy is hit. If i delete the DestroyEnemy() public void then the error disappears and the bullet makes the enemy disappear but the bullet itself just goes through the enemy instead of disappearing when hitting the enemy, how can i fix this. << Code Omitted >>
Make sure the actor is in the world before executing any method(s) that require the actor be in a world or trying to execute any World instance method on the returned World reference from getWorld (it must not be null when trying to execute a method on it). Check using the condition in the following line:
if (getWorld() != null)
This really only needs to be done if there is a chance the actor may not be in the world. The actor is guaranteed to be in the world at the beginning of the act method (unless you yourself code a call to execute the act method).
marcelw123 marcelw123

2020/1/9

#
While i was waiting for the reply i did this and it sorted the error out here is the code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PlayerBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlayerBullet extends Actor
{
    /**
     * Act - do whatever the PlayerBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation(-90);
        move(4);

        CheckEdge();

    } 

    public void CheckEdge()
    {
        if(isAtEdge())
        {                
            getWorld().removeObject(this);                
        }
        else
        if (isTouching(Enemy.class))
        {           
            getWorld().removeObject(this);             
        }
    }

    
}
Now when i hit the enemy the bullet disappears as it should but the enemy doesn't. the enemy only disappears when i get up really close with the player character and shoot from really close. Should i add something to the enemy code, im not sure, im new to coding here is the code for the enemy
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    public void Destroy()
    {
        if (isTouching(PlayerBullet.class))
        {
            getWorld().removeObject(this);
            
        }
    }

}
danpost danpost

2020/1/9

#
You cannot have both enemy and player bullet check for each other like that. If one detects the other and removes itself, the other will not realize they were touching.
You need to login to post a reply.