i want this method to be called by another so that it can check the health of one of the classes i put in there. i was going to just use the parent class, but couldn't figure out how to do it, so i tried doing it in another class and have it call a method from one of the classes based on a switch statement, but i can't remember how to input the switch so it will go to a certain class depending on which the particular unit is calling the method.
here's the code calling the method:
and the code i'm trying to call is this one now:
want it to call that code based on the unit the tower sees, and then get the health of that unit so it can lower the health of that unit when it attacks.
private void attack()
{
List<enemies> e = getObjectsInRange(5, enemies.class);//change radius to variable for when
//upgrading to up radius.
if(! e.isEmpty())
{
/** attack first enemy.
* if(clock)
* {
* e(0).health - attack;
*}
*
**/
//int index = e.indexOf(enemies.class);
enemies temp = e.get(0);
double hp = temp.getHealth();
if(clock())
{
hp -= attack;
}
if(hp <= 0)
{
((tdpath)getWorld()).removeObject(temp);
// minerals+= whatever the value is for the monster.
}
}
}public class spawnHp extends Actor
{
/**
* Act - do whatever the spawnHp wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private Boss b;
private minion1 m1;
private minion2 m2;
private minion3 m3;
private minion4 m4;
private enemies E;
public spawnHp(Boss B, minion1 M1, minion2 M2, minion3 M3, minion4 M4)
{
b = B;
m1 = M1;
m2 = M2;
m3 = M3;
m4 = M4;
}
public void act()
{
// Add your action code here.
}
public double getHealth()
{
E = this.class;
switch (E)
{
case (b)://Boss.class):
{
return Boss.getHealth();
break;
}
case(m1)://minion1.class):
{
return minion1.getHealth();
break;
}
case(m2)://minion2.class):
{
return minion2.getHealth();
break;
}
case(m3)://minion3.class):
{
return minion3.getHealth();
break;
}
case(m4)://minion4.class):
{
return minion4.getHealth();
break;
}
return 0;
}
}
}
