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 when hitting the enemy, how can i fix this.
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); } } }