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

2013/6/2

Enemies with different health number

Fadel Fadel

2013/6/2

#
Hi I wanna do a game where I shoot different enemies with different number of health. I wanna do it neatly so all the removing objects and subtracting health must be in the bullet class. As of now all I got this.
    public void kill()
    {
        destroy(Enemy1.class, 100, 2);
        destroy(Enemy2.class, 100, 2);
    }
    
    public void destroy(Class enemy, int point, int health)
    {
        lives = health;
        Actor hit = getOneIntersectingObject(enemy);
        if(hit != null)
        {
            lives--;
            if (lives == 0)
            {
                getWorld().addObject(explo, getX(), getY());
                getWorld().removeObject(hit);
                points.add(point);
            }
        }
    }
But it keeps forgetting the health count.
It keeps forgetting the health count because every time you call destroy(), you reset "lives" back to 2! Line 9 is the source of your problems here. What I would recommend to do (and is usually a better way to interact with enemy health) is have each enemy have their own health counter inside the class. Then, if a bullet (or whatever you have) hits the enemy, you can call a method inside the enemy class called subtract(), or hurt(), etc. that accepts an int as a parameter, and will subtract that much from it's health. And then the enemy can check every time hurt() is called if it should be dead or not, and if so, it would remove itself. Another cool thing you can do with this is have different kinds of bullets that do different damage to the enemy it hits. And, you'd be calling all the methods from inside the bullet class (like you said you wanted). All you need to do is add the health and hurt/subtract method.
Fadel Fadel

2013/6/2

#
But how can I call enemy class method if the bullet and the enemy class arent connected? How should I do it in my code above?
Use the getOneIntersectingObject() method! Since you have multiple enemies, I'd make a superclass for enemies (called Enemy) that inherits from Actor. This class would have the variables and methods needed to make an enemy. Then you have any enemies you want to add to the game be subclasses of Enemy. Then when you want to hurt an enemy, have this code in your bullet class:
public void act()
{
    ...
    hurtEnemy();
}

public void hurtEnemy()
{
    Enemy e = (Enemy)getOneIntersectingObject(Enemy.class); //This will return any intersecting Enemy, even if it's a subclass
    if (e != null)
    {
        e.hurt(damage);  //It'd be smart for bullet to have a variable called damage, that is defined in it's constructor
        getWorld().removeObject(this);
    }
}
And that is how you connect bullet with enemy.
Fadel Fadel

2013/6/2

#
So my Enemies super class looks like this :
public class Enemies extends Actor
{
  private int health;

public void loseHealth()
    {
        health--;
    }
}
and my enemy subclass:
public class Enemy1 extends Enemies
{
    private int health = 0;
    
    public void act() 
    {
        move(-2);
        checkHealth();
    }     
    
    public void Health()
    {
        loseHealth();
    }
    
    public void checkHealth()
    {
        if (health == 2)
        {
            explosion(4);
            points(100);
            getWorld().removeObject(this);
        }
}
    }
But it doesnt work.
Because you're trying to use a private variable in a non-private place (and you also set the health to zero, so it would never reach health == 2). What you should do is have a constructor for Enemies like this:
public Enemies(int hp)
{
    health = hp;
}
and then for Enemy1 do this for the constructor:
public Enemy1()
{
    super(/*Insert Health Here*/);
}

//And to fix your checkHealth() method
public void checkHealth()
{
    if (health <= 2)
    {
        ...
    }
}
Fadel Fadel

2013/6/3

#
Thanks got it working ! You saved me :D
You need to login to post a reply.