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

2013/3/5

Health for enemies

1
2
henrYoda henrYoda

2013/3/5

#
Hi guys, I need help with adding health to my enemies, and then decreasing it when a bullet hits it. Does anyone know how to do this? Here is the code for the enemy
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Key extends Actor
{

   /**
     * Create a new key.
     */
public void act()
{
    movementAi();
    
   }
  //movement  
  
  public void movementAi() 
  {
      
    move(2);
    if(Greenfoot.getRandomNumber(100) < 10)
    {
        turn(Greenfoot.getRandomNumber(90));   
    
    }
    if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
    {
        turn(180);
    
    }
    if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
    {
       turn(180);
    
    }
 
}
    
    
}
And here is the code for the bullet
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
{
 public void act() 
    {
        move(7);
        disappear();
        if (getWorld() == null)return;
        collisionDetection();
        //colision detection and point awarding
        
        
        
        //create bullet object
    }
   public void collisionDetection()
   {
    Actor Key = getOneIntersectingObject(Key.class);
            if (Key != null) 
               { // We've hit an asteroid!
                hitObject();
                getWorld().removeObject(Key);
                getWorld().removeObject(this);
            
    }
}    //make bullet disappear
 public void disappear()
 {
if( getX() <= 4 || getX() >= getWorld().getWidth()-2) 
    
        {
            getWorld().removeObject(this);
            return;
        }
      if( getY() <= 4 || getY() >= getWorld().getHeight()-2) 

        {
            getWorld().removeObject(this);
            return;
        }
    
}      
     
    
    public void hitObject()
    {
        
        menu menuWorld = (menu) getWorld();
        Counter counter = menuWorld.getCounter();
        counter.scoreCount(5);
        
         
            
            
                
            
           
        }
    }



Gevater_Tod4711 Gevater_Tod4711

2013/3/5

#
First your enemy has to have a health variable which you can count down. Then if your bullet hits the enemy you have to decrement this value:
public class Enemy extends Actor {
    private int health = 5;//or any other starting value;
    
    public void setHealth(int points) {
        health += points;
    }
    public int getHealth() {
        returnt health;
    }
}


//in your bullet class you have to add this method:
public void hitEnemy() {
    Enemy enemy = (Enemy) getOneObjectAtOffset(0, 0, Enemy.class);
    if (enemy != null) {
        enemy.setHealth(-1);//decrements the health of this enemy;
        //if you want your bullet to be removed now you have to add the next line; otherwhise delete it.
        getWorld().removeObject(this);
    }
}
henrYoda henrYoda

2013/3/5

#
I have a problem though, when I add line 17 it says that the setHealth method does not exist?
henrYoda henrYoda

2013/3/5

#
no worries, it works now :)
Nostic Nostic

2013/5/17

#
What do you do if you have two enemies?
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
What do you mean by two enemys? Two classes for enemys? Or two enemys that get hit at the same time?
Nostic Nostic

2013/5/17

#
Two classes
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
Then you have to do the same for each class (find the intersecting object and hit it). You just have to change the class name. Or if your enemys are sublcasses of the same superclass you can use the superclass name. But you should only do this if the enmeys are the only subclasses. Otherwhile every other thing that get hit by the bullet will be destroyed. (E.g. if you use Actor every actor will be hurt by your bullet)
Nostic Nostic

2013/5/17

#
I did this in my game public void hitAlien1() { Alien1 enemy = (Alien1) getOneObjectAtOffset(0, 0, Alien1.class); if (enemy != null) { enemy.setHealth(-1); //Siger at den skal miste et liv getWorld().removeObject(this); //Projektilet skal forsvinde når den rammer modstanderen } public void hitAlien2() { Alien2 enemy2 = (Alien2) getOneObjectAtOffset(0, 0, Alien2.class); if (enemy2 !=null) { enemy2.setHealth2(-1); getWorld().removeObject(this); } } But I get a error :S
Nostic Nostic

2013/5/17

#
Sorry, I'm very bad at greenfoot currently, so all the help I could get would be great!
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
The code almost is right but you've forgot to close the first method. If you add a '}' after line 7 it should work.
Nostic Nostic

2013/5/17

#
I get this error now: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Laser.hitAlien2(Laser.java:47) at Laser.act(Laser.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
Nostic Nostic

2013/5/17

#
But how do I set up the code if put in actor?
Gevater_Tod4711 Gevater_Tod4711

2013/5/17

#
The code means that your actor get's removed and then trys to call this method. But that doesn't work if the actor is not in the world. Could you post the code of the current act method of your Laser class.
Nostic Nostic

2013/5/17

#
public class Laser extends Animal
{
    public int speed = 14;
    public void act() 
    {
        setLocation(getX() + speed, getY()); 
        if(getX()>790)
        {
            getWorld().removeObject(this);
            return;
        }
        kollisionDetection();
        hitAlien1();
        hitAlien2();
    }    
   
    
   public void kollisionDetection()  
   {  
        Actor bullet = getOneIntersectingObject(Laser.class);  
            if (bullet != null)   
               { 
                getWorld().removeObject(bullet);  
                getWorld().removeObject(this);  
            }
    }
   public void hitAlien1() 
   {  
       Alien1 enemy = (Alien1) getOneObjectAtOffset(0, 0, Alien1.class);  
            if (enemy != null) {  
                enemy.setHealth(-1); //Siger at den skal miste et liv
                getWorld().removeObject(this); //Projektilet skal forsvinde når den rammer modstanderen
            }
        }
   public void hitAlien2()
   {           
       Alien2 enemy2 = (Alien2) getOneObjectAtOffset(0, 0, Alien2.class);
            if (enemy2 !=null) {
                enemy2.setHealth2(-1);
                getWorld().removeObject(this);
            }  
   }  
}
There are more replies on the next page.
1
2