This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
KauTC
KauTC wrote ...

2017/11/12

Health for Emeny

KauTC KauTC

2017/11/12

#
Do i have to make another class for the enemy's health or i can just create it inside the enemy class itself. How do i do the codes for it ? My idea is that the enemy will have 2 health if the enemy is hit by my bullet the bullet will disappear and the enemy will minus 1 hp. If the enemy is hit again by the bullet the enemy will then disappear. How do i write the code for this scenario.
Super_Hippo Super_Hippo

2017/11/12

#
It depends if you want to display the health or not. If not, then you won't need a different class and you can have a variable in the enemy class to track its health.
//Enemy
private int health = 2;

public void loseHealth(int amount)
{
    health -= amount;
    if (health < 1)
    {
        getWorld().removeObject(this);
    }
}
//Bullet
public void act()
{
    move(2);
    Enemy e = (Enemy) getOneIntersectingObject(Enemy.class);
    if (e != null)
    {
        e.loseHealth(1);
        getWorld().removeObject(this);
    }
}
KauTC KauTC

2017/11/12

#
No i do not wish to display the health. How do i do so that i can minus the health when the bullet touches the monster and then the bullet disappear but the monster still move. If the bullet touches the monster again the monster then will disappear This is my monster's code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Second here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Second extends Monster
{
    private int horizontalSpeed = -3;
    private int verticalSpeed = 4;
    /**
     * Act - do whatever the First wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
       bounceAtEdge();
       move(horizontalSpeed , verticalSpeed );
   }
   
   public void move(int changeX, int changeY)   
   {
       int x = getX();
       int y = getY();
       int newX = x + changeX;
       int newY = y + changeY;
       setLocation(newX, newY);
    }
    
   public String hitTheEdge()
   {
       int x  = getX();
       int y = getY();
       World myWorld = getWorld();
       int rightSide = myWorld.getWidth() -1;
       int bottomSide = myWorld.getHeight() -1;
       if(y == 0)
       {
           return "top";
        }else if (x == 0)
        {
            return "left";
        }else if(x == rightSide) {
            return "right";
        }else if (y == bottomSide)
        {
            return "bottom";
        }else {
            return null;
        }
   }
   
    public void bounceAtEdge()
   {
       String edge = hitTheEdge();
       if(edge == "top" || edge == "bottom")
       {
           verticalSpeed = verticalSpeed * -1;
        }else if (edge == "left" || edge == "right")
        {
            horizontalSpeed = horizontalSpeed * -1;
        }
   }  
   
}
This is my bullet's code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Second here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Second extends Monster
{
    private int horizontalSpeed = -3;
    private int verticalSpeed = 4;
    /**
     * Act - do whatever the First wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
       bounceAtEdge();
       move(horizontalSpeed , verticalSpeed );
   }
   
   public void move(int changeX, int changeY)   
   {
       int x = getX();
       int y = getY();
       int newX = x + changeX;
       int newY = y + changeY;
       setLocation(newX, newY);
    }
    
   public String hitTheEdge()
   {
       int x  = getX();
       int y = getY();
       World myWorld = getWorld();
       int rightSide = myWorld.getWidth() -1;
       int bottomSide = myWorld.getHeight() -1;
       if(y == 0)
       {
           return "top";
        }else if (x == 0)
        {
            return "left";
        }else if(x == rightSide) {
            return "right";
        }else if (y == bottomSide)
        {
            return "bottom";
        }else {
            return null;
        }
   }
   
    public void bounceAtEdge()
   {
       String edge = hitTheEdge();
       if(edge == "top" || edge == "bottom")
       {
           verticalSpeed = verticalSpeed * -1;
        }else if (edge == "left" || edge == "right")
        {
            horizontalSpeed = horizontalSpeed * -1;
        }
   }  
   
}
Super_Hippo Super_Hippo

2017/11/12

#
(You posted the same code twice.) Why don't you try what I gave you? It should do exactly that. (Change "Enemy" to "Monster".)
KauTC KauTC

2017/11/12

#
Sorry i tried the code but it doesnt work Sorry here is the bullet code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lattack here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lattack extends Character
{
    /**
     * Act - do whatever the Lattack wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-5);
        eatFirst();
    }
    
    public boolean atWorldEdge()
   {
        if(getX() <= 10 || getX() > getWorld().getWidth() - 10)
        return true;
        if(getY() <= 10 || getY() > getWorld().getHeight() - 2)
        return true;
        else 
        return false;
   }  
   
      public void eatFirst()
   {
      Actor First;
      First = getOneIntersectingObject(First.class);
      Actor Second;
      Second = getOneIntersectingObject(Second.class); 
      Actor Third;
      Third = getOneIntersectingObject(Third.class);
      if(First != null)
      {
           World myWorld = getWorld();
           getWorld().removeObject(First);
           Background background = (Background)myWorld;
           Counter counter = background.getCounter();
           counter.addScores();
           getWorld().removeObject(this);
      }else if(Second != null)
      {
           World myWorld = getWorld();
           getWorld().removeObject(Second);
           Background background = (Background)myWorld;
           Counter counter = background.getCounter();
           counter.addScores();
           getWorld().removeObject(this);
      }else if(Third != null)
      {
           World myWorld = getWorld();
           getWorld().removeObject(Third);
           Background background = (Background)myWorld;
           Counter counter = background.getCounter();
           counter.addScores();
           getWorld().removeObject(this);
      }else if(atWorldEdge())
      {
           getWorld().removeObject(this);
      }
   }    
}
danpost danpost

2017/11/12

#
You will need to add a field in the Monster (or Second) class to track bullet hit(s). It could be a boolean tracking previously hit or an int counting each hit (depending on how many hits you are allowing). Are your monsters truly bouncing off the edges? Seems like you may have some issue with that (but, maybe not). Your at world edge offset values are not consistent in the Lattack class. Was that intentional? The 'eatFirst' method can probably be simplified. If nothing else, the last 'if' (line 63) should probably not have the 'else' in front of it and be started on a new line.
You need to login to post a reply.