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

2023/2/10

Kill multiple actors with a health system in place

Deepfist Deepfist

2023/2/10

#
Hello, I am trying to make a kill system where, if a bullet hits the Stalker or Sniper, it kills them when their health is low enough. With the Sniper it works, but with the stalker I get the error : java.lang.NullPointerException at Player_Bullet.Stalkerkill(Player_Bullet.java:30) at Player_Bullet.werk(Player_Bullet.java:53) at Player_Bullet.act(Player_Bullet.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) The code for both is the same, but with different names and it references the right Actor. The codes for the involved actors will be standing below (The actors it extends to have nothing to do with the code itself) This is the code of the bullet that kills the enemies (not the class Enemies)
import greenfoot.*;  
public class Player_Bullet extends Bullet
{
    public Sniper sniper;
    public Stalker stalker;
    public boolean deleted = false;
    public int Deletecount = 0;
    
    public Player_Bullet (Sniper sniperDirk) {
        sniper = sniperDirk;
    }
    public Player_Bullet (Stalker stalkerDirk) {
        stalker = stalkerDirk;
    }
    public void act()
    {
        werk();
        Checkdelete();
    }
    
    public void Sniperkill() {
      if (isTouching(Sniper.class) && sniper.Sniperhealth == 1 && deleted ==
      false) {
          getWorld().removeObject(sniper);
          getWorld().removeObject(this);
          deleted = true;
      }
    }
    public void Stalkerkill() {
        if (isTouching(Stalker.class) && stalker.Stalkerhealth == 1 && deleted
        == false) {
          getWorld().removeObject(sniper);
          getWorld().removeObject(this);
          deleted = true;
      }
    }
    public void Checkdelete() {
      if (deleted == true) {
            Deletecount++;
        }
        else {
            Sniperkill();
        }
      if (Deletecount == 1) {
            deleted = false;
        }
    }
    public void werk() {
      if (deleted == true) {
            Deletecount++;
        }
        else {
            Stalkerkill();
        }
      if (Deletecount == 1) {
            deleted = false;
        }
    }
With this actor it works
import greenfoot.*;  
/**
 *
 */
public class Sniper extends Enemies
{
    public int Sniperhealth = 3;
    public Sniper() {
        UpdatehealthSniper(0);
    }
    public void act() 
    {
        Sniperhit();
        movementTest();
    }
    public void Sniperhit() {
        if (isTouching(Player_Bullet.class) && Sniperhealth == 3) {
            Sniperhealth = 2;
            removeTouching(Player_Bullet.class);
        }
        if (isTouching(Player_Bullet.class) && Sniperhealth == 2) {
            Sniperhealth = 1;
            removeTouching(Player_Bullet.class);
        }
    }
    public void UpdatehealthSniper(int CheckhealthSniper) {
        Sniperhealth += CheckhealthSniper;
    }
    }
With this actor it doesn't work
import greenfoot.*;  
/**
 * 
 */
public class Stalker extends Enemies
{
    public int Stalkerhealth = 3;
    public Stalker() {
        UpdatehealthStalker(0);
    }
    public void act()
    {
        movementTest();
        Stalkerhit();
    }
    public void Stalkerhit() {
        if (isTouching(Player_Bullet.class) && Stalkerhealth == 3) {
            Stalkerhealth = 2;
            removeTouching(Player_Bullet.class);
        }
        if (isTouching(Player_Bullet.class) && Stalkerhealth == 2) {
            Stalkerhealth = 1;
            removeTouching(Player_Bullet.class);
        }
    }
    public void UpdatehealthStalker(int CheckhealthStalker) {
        Stalkerhealth += CheckhealthStalker;
    }
}
This is the code of myWorld, it might have something to do with enemies in Player_Bullet, but when I put in stalker it still doesn't work. In fact I get a new error.
/**
 * 
 */
public class MyWorld extends World
{
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(900, 900, 1); 
        prepare();
    }
    
    
    private void prepare()
    {
        Stalker stalker = new Stalker();
        addObject(stalker,441,121);
        Sniper sniper = new Sniper();
        addObject(sniper,244,124);
        Player player = new Player();
        addObject(player,234,484);
        Enemy_Bullet enemy_Bullet1 = new Enemy_Bullet(player);
        addObject(enemy_Bullet1,800,100);
        Enemy_Bullet enemy_Bullet2 = new Enemy_Bullet(player);
        addObject(enemy_Bullet2,800,200);
        Enemy_Bullet enemy_Bullet3 = new Enemy_Bullet(player);
        addObject(enemy_Bullet3,800,300);
        Enemy_Bullet enemy_Bullet4 = new Enemy_Bullet(player);
        addObject(enemy_Bullet4,800,400);
        Player_Bullet player_Bullet = new Player_Bullet(sniper);//or stalker 
        addObject(player_Bullet,100,100);
        Player_Bullet player_Bullet1 = new Player_Bullet(sniper);
        addObject(player_Bullet1,100,200);
        Player_Bullet player_Bullet2 = new Player_Bullet(sniper);
        addObject(player_Bullet2,100,300);
    }
}
the error java.lang.NullPointerException at Player_Bullet.Sniperkill(Player_Bullet.java:22) at Player_Bullet.Checkdelete(Player_Bullet.java:42) at Player_Bullet.act(Player_Bullet.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) movementTest is just a movement code, it has nothing to do with the errors I use the same code as the sniper for my player.class, and it just works fine (with name changes ofc.)
danpost danpost

2023/2/10

#
Deepfist wrote...
I am trying to make a kill system where, if a bullet hits the Stalker or Sniper, it kills them when their health is low enough. With the Sniper it works, but with the stalker I get the error : << Error Trace Omitted >> The code for both is the same, but with different names and it references the right Actor. The codes for the involved actors will be standing below (The actors it extends to have nothing to do with the code itself) This is the code of the bullet that kills the enemies (not the class Enemies) << Code Omitted >> This is the code of myWorld, it might have something to do with enemies in Player_Bullet, but when I put in stalker it still doesn't work. In fact I get a new error. << Code Omitted >> the error << Error Trace Omitted >> movementTest is just a movement code, it has nothing to do with the errors I use the same code as the sniper for my player.class, and it just works fine (with name changes ofc.)
I am not sure all that code is needed. If you need to link the bullets to whomever shot them, then use inner classes for that. In general, all any bullet should do is move and get removed upon hitting an edge. So, maybe have the following class:
import greenfoot.*;

public class Projectile extends Actor
{
    protected int speed;
    
    public void act()
    {
        move(speed);
        if (isAtEdge()) getWorld().removeObject(this);
    }
}
and, for example, for the player:
import greenfoot.*;

public class Player extends Actor
{
    // usually stuff
    
    // followed by (still in Player class)
    public class Bullet extends Projectile
    {
        public Bullet()
        {
            setImage(/** player bullet image filename */);
            speed = 5; // adjust as needed
        }
}
A Bullet object created by a Player object is a Player.Bullet object, The same can be done with the Enemies class -- add an inner class for their bullets. Cut all similar codes from Sniper and Stalker and put them in the Enemies class (including the health field, whose value can be set in the constructors of the specific subclasses, if needed). Instead of having the bullets look for their targets, have the targets look for the bullets. That way, the class of the target deals with the collision, which is way easier to code. Not sure why you are creating bullets in your MyWorld constructor. The Actor objects that shoot the bullets should create them (I would think).
You need to login to post a reply.