So currently I have a working system for my player.class, where if the bullet hits the player, then the bullet removes 1 health from the player.class. This works fine and when the health from the player == 0, then the player will kill (remove) himself with dead();
However, I also want to add this to my enemies, at first this worked fine as the enemies.class was only 1 class and the PlayerBullet.class was only designed to kill the enemies.class. I wanted to make subclasses for the enemies.class with different health values. Now when a PlayerBullet hits an enemy, it gives an error. This is because it is forced to remove itself. Now, instead of giving an error, whenever I reset my game, the health value of the enemy I hit is zero, which means that it will kill itself imidiately, incase my dead(); code is active. If it isn't active then the bullet will just pass trough it without doing anything
The code for my EnemyBullet (against player)
This works just fine as it is only against the Player.class
This is the Current code for the PlayerBullet (against enemies) It isn't finished as it wouldn't really work for me
The code is exactly the same, except for the names. Yet it doesn't work.
Speed is just movement and not important
I'd also like to add that the health values are static and they come from the class Enemies. Stalker, Shooter and Sniper are subclasses of Enemies.
import greenfoot.*; public class EnemyBullet extends Projectile { public void act() { Hit(); //move(Speed); //Speed is een static int van Projectile } public void Hit() { Player player = (Player)getOneIntersectingObject(Player.class); if(player != null && player.Playerhealth != 0) { player.Playerhealth--; getWorld().removeObject(this); } } }
import greenfoot.*; public class PlayerBullet extends Projectile { public void act() { move(Speed); Hit(); } public void Hit() { Shooter shot = (Shooter)getOneIntersectingObject(Shooter.class); Sniper snip = (Sniper)getOneIntersectingObject(Sniper.class); Stalker stal = (Stalker)getOneIntersectingObject(Stalker.class); if(stal != null && stal.StalkerHealth != 0) { stal.StalkerHealth--; getWorld().removeObject(this); } if(snip != null && snip.SniperHealth != 0) { snip.SniperHealth--; getWorld().removeObject(this); } if(shot != null && shot.ShooterHealth != 0) { shot.ShooterHealth--; getWorld().removeObject(this); } } }