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

2019/6/1

Adding Health to Enemy

1
2
Verglas Verglas

2019/6/1

#
Hi! I'm having trouble with adding health to my enemy units and decreasing it when it hits the bullet. I'm using the regular ghost that will die in 1 hit as the parent to the bossGhost that will die in 5 hit and tried to set the health of the bossGhost to 5 using setter and getter. I don't know if I'm doing it right or if I need to do something else. Here is the code for my ghost.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ghost here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ghost extends Actor
{
    /**
     * Act - do whatever the ghost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    protected int ghostSpeed = 1;
    public int ghostHealth = 1;
    public void act() 
    {
        moveAuto(ghostSpeed);
        hitBorder();
    }    
    
    public int currGhostHealth()
    {
        return ghostHealth;
    }
    
    public void setGhostHealth(int Health)
    {
        this.ghostHealth = ghostHealth;
    }
    
    public void minGhostHealth(int minHealth)
    {
        this.ghostHealth = ghostHealth - minHealth;
    }
    
    public void moveAuto(int ghostSpeed)
    {
        setLocation(getX(), getY() + ghostSpeed);
    }
    
    public int hitCount;
    
    public void hitBorder()
    {
        Actor border = getOneIntersectingObject(houseBorder.class);
        
        if(border != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            
            Greenfoot.delay(5);
            Greenfoot.setWorld(new GameOverScreen());
        }
    }
}
Here is the code to my evilBoss with 5 health.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class evilGhost here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class evilGhost extends ghost
{
    /**
     * Act - do whatever the evilGhost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAuto(ghostSpeed);
        evilGhost1();
    }    
    
    public void evilGhost1()
    {
        setGhostHealth(5);
    }
}
Here is my bullet code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
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.
     */
    int maxDistanceTravelled = 100;
    //private int speed = 5;
    public void act()
   {
       move(4);
       destroyEnemies();
       checkBoundaries();
       bulletRange();
   }  
  
   //we add a method "checkBoundaries()" that destroys bullets that are off screen.
   public void checkBoundaries()
   {
       if (getWorld() == null) return;
       World myWorld = getWorld();
       if(this != null){
       if(getX() > myWorld.getWidth() - 1) 
            myWorld.removeObject(this);
       else if(getX() < 1) 
            myWorld.removeObject(this);
       else if(getY() > myWorld.getHeight() - 1) 
            myWorld.removeObject(this);
       else if(getY() < 1) 
            myWorld.removeObject(this);
       }     
   }
   
   //"destroyEnemies()" destroys enemies.
   public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy.
       if (getWorld() == null) return;
       if(this != null)
       {
           Actor ghost = getOneIntersectingObject(ghost.class);
           if(ghost != null) 
           {
               World myWorld = getWorld();
               //myWorld.removeObject(ghost);
               ghost Ghost = new ghost();
               Ghost.minGhostHealth(1);
               
               if(Ghost.currGhostHealth() == 0)
               {
                   MyWorld myworld = (MyWorld)myWorld;
                   Score score = myworld.getScore();
                   score.addScore();
                   myWorld.removeObject(ghost);
                   myWorld.removeObject(this);
               }
           }
        }
   }
   
   public void bulletRange(){
       if (getWorld() == null) return;
       maxDistanceTravelled -= 1;
       if(maxDistanceTravelled == 0)
       {
           getWorld().removeObject(this);
       }
    }
}

Thank you very much :D.
danpost danpost

2019/6/1

#
Look closely at what you are doing at line 30 in the ghost class.
danpost danpost

2019/6/1

#
Also, line 18 in the evilGhost class is constantly setting health to 5. Use a constructor block to initialize the health.
Verglas Verglas

2019/6/1

#
Thank you so much for the reply. I've changed the evilGhost class into the following:
public class evilGhost extends ghost
{
    /**
     * Act - do whatever the evilGhost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAuto(ghostSpeed);
    }    
    
    public evilGhost()
    {
        setGhostHealth(5);
    }
}
And this is the ghost class. protected int ghostSpeed = 1; public int ghostHealth = 1; public void act() { moveAuto(ghostSpeed); hitBorder(); } public int currGhostHealth() { return ghostHealth; } public void setGhostHealth(int Health) { this.ghostHealth = Health; } public void minGhostHealth(int minHealth) { this.ghostHealth = ghostHealth - minHealth; } public void moveAuto(int ghostSpeed) { setLocation(getX(), getY() + ghostSpeed); } public int hitCount; public void hitBorder() { Actor border = getOneIntersectingObject(houseBorder.class); if(border != null) { World myWorld = getWorld(); myWorld.removeObject(this); Greenfoot.delay(5); Greenfoot.setWorld(new GameOverScreen()); } } } But the evilGhost is still one hit to death. Is there still something wrong with my evilGhost class or ghost class?
Verglas Verglas

2019/6/1

#
A little update, I've changed the minGhostHealth method in the ghost class with this.
public void minGhostHealth(int minHealth)
    {
        this.ghostHealth -= minHealth;
    }
Verglas Verglas

2019/6/1

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ghost here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ghost extends Actor
{
    /**
     * Act - do whatever the ghost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    protected int ghostSpeed = 1;
    public int ghostHealth = 1;
    public void act() 
    {
        moveAuto(ghostSpeed);
        hitBorder();
    }    
    
    public int currGhostHealth()
    {
        return ghostHealth;
    }
    
    public void setGhostHealth(int Health)
    {
        this.ghostHealth = Health;
    }
    
    public void minGhostHealth(int minHealth)
    {
        this.ghostHealth -= minHealth;
    }
    
    public void moveAuto(int ghostSpeed)
    {
        setLocation(getX(), getY() + ghostSpeed);
    }
    
    public int hitCount;
    
    public void hitBorder()
    {
        Actor border = getOneIntersectingObject(houseBorder.class);
        
        if(border != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            
            Greenfoot.delay(5);
            Greenfoot.setWorld(new GameOverScreen());
        }
    }
}
Super_Hippo Super_Hippo

2019/6/1

#
In line 54 of the Bullet class, you create a NEW ghost, decrease its health by 1, then check if it is 0 (which it is because your ghosts have 1 health) and then remove the bullet and the ghost which it is intersecting no matter what health this one had.
Verglas Verglas

2019/6/1

#
How do I decrease the health of the already existing ghost?
Super_Hippo Super_Hippo

2019/6/1

#
In line 49, you get a reference to the ghost object which is touched by the bullet. Use this reference instead and don't create a new ghost there at all.
Verglas Verglas

2019/6/1

#
But I can't access the minGhostHealth through that reference. Is this what you mean?
public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy.
       if (getWorld() == null) return;
       if(this != null)
       {
           Actor ghost = getOneIntersectingObject(ghost.class);
           if(ghost != null) 
           {
               World myWorld = getWorld();
               //myWorld.removeObject(ghost);
               
               ghost.minGhostHealth(1); //this became error. It said "cannot fine symbol - method minGhostHealth(int).
               
               if(ghost.currGhostHealth() == 0) //this one is error as well.
               {
                   MyWorld myworld = (MyWorld)myWorld;
                   Score score = myworld.getScore();
                   score.addScore();
                   myWorld.removeObject(ghost);
                   myWorld.removeObject(this);
               }
           }
        }
   }
danpost danpost

2019/6/1

#
On line 7, change Actor to ghost for minGhostHealth to be accessible.
Verglas Verglas

2019/6/1

#
When I change it, I got an error message "Incompatible types: greenfoot.Actor cannot be converted to ghost".
public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy.
       if (getWorld() == null) return;
       if(this != null)
       {
           ghost ghost = getOneIntersectingObject(ghost.class);
           if(ghost != null) 
           {
               World myWorld = getWorld();
               //myWorld.removeObject(ghost);
               ghost Ghost = new ghost();
               Ghost.minGhostHealth(1);
               
               if(Ghost.currGhostHealth() == 0)
               {
                   MyWorld myworld = (MyWorld)myWorld;
                   Score score = myworld.getScore();
                   score.addScore();
                   myWorld.removeObject(ghost);
                   myWorld.removeObject(this);
               }
           }
        }
   }
danpost danpost

2019/6/1

#
Oh, yeah, you need to begin the right side (after the equal sign) with (ghost).
Verglas Verglas

2019/6/2

#
Thanks! The error is gone, but the evilGhost unit is still removed after hit by the bullet once. I wanted it so that it will be destroyed after getting hit y the bullet 5 times, hence the setGhostHealth(5). Is there a way to do that?
public class ghost extends Actor
{
    /**
     * Act - do whatever the ghost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    protected int ghostSpeed = 1;
    public int ghostHealth = 1;
    public void act() 
    {
        moveAuto(ghostSpeed);
        hitBorder();
    }    
    
    public int currGhostHealth()
    {
        return ghostHealth;
    }
    
    public void setGhostHealth(int Health)
    {
        this.ghostHealth = Health;
    }
    
    public void minGhostHealth(int minHealth)
    {
        this.ghostHealth -= minHealth;
    }
    
    public void moveAuto(int ghostSpeed)
    {
        setLocation(getX(), getY() + ghostSpeed);
    }
    
    public int hitCount;
    
    public void hitBorder()
    {
        Actor border = getOneIntersectingObject(houseBorder.class);
        
        if(border != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            
            Greenfoot.delay(5);
            Greenfoot.setWorld(new GameOverScreen());
        }
    }
}
public class evilGhost extends ghost
{
    /**
     * Act - do whatever the evilGhost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAuto(ghostSpeed);
        hitBorder();
    }    
    
    public evilGhost()
    {
        setGhostHealth(5);
    }
}
This is the bullet class.
public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy.
       if (getWorld() == null) return;
       if(this != null)
       {
           ghost ghost = (ghost) getOneIntersectingObject(ghost.class);
           if(ghost != null) 
           {
               World myWorld = getWorld();
               //myWorld.removeObject(ghost);
               //ghost Ghost = new ghost();
               ghost.minGhostHealth(1);
               
               if(ghost.currGhostHealth() == 0)
               {
                   MyWorld myworld = (MyWorld)myWorld;
                   Score score = myworld.getScore();
                   score.addScore();
                   myWorld.removeObject(ghost);
                   myWorld.removeObject(this);
               }
           }
        }
   }
   
   public void bulletRange(){
       if (getWorld() == null) return;
       maxDistanceTravelled -= 1;
       if(maxDistanceTravelled == 0)
       {
           getWorld().removeObject(this);
       }
    }
Verglas Verglas

2019/6/2

#
An update, the evilGhost class is still deleted after getting hit by the bullet once. Is there a way to fix this persisting problem???
There are more replies on the next page.
1
2