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

2015/12/19

How to get two sub-class to communicate

XcaNIa XcaNIa

2015/12/19

#
So I have a health Variable in each enemies, and I have a damage value in the projectile class. I am quite lost. Is there a way so that i can make the projectile take health away from the enemy?
Super_Hippo Super_Hippo

2015/12/19

#
In the projectile class, you somewhere check if it intersects/touches an enemy. If every enemy class is a subclass of 'Enemy' and you declare the variable 'health' in that class, you can do it like this:
Enemy e = (Enemy) getOneIntersectingObject(Enemy.class);
if (e != null)
{
    //if health is public:
    e.health--;
    if (e.health < 1)
    {
        getWorld().removeObject(e);
    }
    
    //OR,  if health is private:
    e.setHealth(e.getHealth()-1);
    if (e.getHealth() < 1)
    {
        getWorld().removeObject(e);
    }
}
In the second case (if health is private), you need these methods in the Enemy class:
public void setHealth(final int newHealth)
{
    health = newHealth;
}

public int getHealth()
{
    return health;
}
fejfo fejfo

2015/12/19

#
Super_Hippo wrote...
public void setHealth(final int newHealth)
can you explain what the final keyword means there usaully it means it can't be changed but there you do change it
Super_Hippo Super_Hippo

2015/12/19

#
'final' means that it can't be changed, that's true. And It is not changed there. In line 3, 'health' is set to 'newHealth', not the other way around. Of course the code would also work without 'final', but I usually use it for variables which shouldn't be changed, especially as parameters in methods. Maybe the scenario is less likely to lag because of this code improvement. :D
XcaNIa XcaNIa

2015/12/19

#
Thanks, will try this
XcaNIa XcaNIa

2015/12/19

#
Hm, does not seem to work it can not find the variable NewHP, which is what is stored in each Enemy class. Just a bit more info if needed. The Enemies.class, what i called it, does not contain anything but a few methods which buffs the baseHP. The newHP stored in calculated by using the said methods.
XcaNIa XcaNIa

2015/12/20

#
Ok, I figured that thing out. I put this in to a method to use for each projectile. But i am getting this error now. It says the the method is static so it can not modify the NewHP as it is non static.
public void destroyEnemies(double Damage)  {      
         
        
        Actor wedge = getOneIntersectingObject(Wedge.class);
        Actor boss_wedge = getOneIntersectingObject(Boss_Wedge.class);
        Actor tank = getOneIntersectingObject(Tonk.class);
        Actor boss_tank = getOneIntersectingObject(Boss_Tonk.class);
        Actor box = getOneIntersectingObject(Box.class);
        Actor boss_box = getOneIntersectingObject(Boss_Box.class);
        Actor mega = getOneIntersectingObject(Mega.class);
        Actor super_boss_mega  = getOneIntersectingObject(SuperBoss_Mega.class);
        
        if (wedge != null){
            Wedge.NewHP -= Damage;
            getWorld().removeObject(this);
        }
XcaNIa XcaNIa

2015/12/20

#
I removed the Wedge.NewHP -= Damage as a test. However, now it throws NullPointerException
danpost danpost

2015/12/20

#
'Wedge' is the name of your class; 'wedge' (starting with lowercase 'w') is the name you gave the intersecting Wedge object (line 4). So, use:
wedge.NewHP -= Damage;
Super_Hippo Super_Hippo

2015/12/20

#
XcaNIa wrote...
it can not find the variable NewHP, which is what is stored in each Enemy class.
You can remove that from all Enemy subclasses and write it into the Enemy class instead. Every subclass inherits it then.
XcaNIa XcaNIa

2015/12/20

#
Super_Hippo wrote...
XcaNIa wrote...
it can not find the variable NewHP, which is what is stored in each Enemy class.
You can remove that from all Enemy subclasses and write it into the Enemy class instead. Every subclass inherits it then.
So how do i make it have different health? Use od constructor of some sort? if i put wedge.NewHP -= Damage, it can not find NewHP
Super_Hippo Super_Hippo

2015/12/20

#
Use the following line instead of lines 4 through 11 and change 'wedge' in lines 12 and 13 to 'enemy'.
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
XcaNIa XcaNIa

2015/12/20

#
Super_Hippo wrote...
Use the following line instead of lines 4 through 11 and change 'wedge' in lines 12 and 13 to 'enemy'.
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
Thanks, but i have tired this already. And it does not do what i wanted it to do
You need to login to post a reply.