Hi, I'm having a bit fo trouble with my code and I don't understand why it doesn't work. I just need help getting it to work to continue with my game.
Basically I have an enemy that creates this Bullet object and once the bullet hits a wall, player, or edge of the world, it gets destroyed. My problem is that once it gets destroyed, the code still tries to run and an error pops up saying "Actor not in world."
Heres my code:
I tried to get the code to work through the use of the if statements that ask if the bullet in the world or not... but it didnt work.
public class Bullet extends Actor
{
/**
* Act - do whatever the Bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Bullet.class != null)
{
move(-3);
BulletCollision();
}
}
public Bullet()
{
GreenfootImage image = getImage();
image.scale(20,20);
setImage(image);
}
public void BulletCollision()
{
if (Bullet.class != null)
{
Actor Wall = getOneIntersectingObject(Wall.class);
if (Wall != null)
{
getWorld().removeObject(this);
}
if (getX() == 0 || getX() == 399)
{
getWorld().removeObject(this);
}
Actor Player = getOneIntersectingObject(Player.class);
if (Player != null)
{
getWorld().removeObject(this);
}
}
}
}
