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

2019/5/25

hp problems

loldertroll loldertroll

2019/5/25

#
Hey guys. I am making a scenario about killing bosses and proceeding to the next level. Because its a game about fighting i have hp involved. I wanted to create a boss that has a special attack which gets triggered everytime after loosing 5 hp but i dont know how to do it. I made the hp with "private int hp=50". Ty for helping.
Super_Hippo Super_Hippo

2019/5/25

#
After losing health, you can do this:
if (hp == 0)
{
    //die
}
else if (hp % 5 == 0)
{
    //trigger special attack
}
This only works when it always loses one health. If he can lose several ones at once, then you will need something like this:
private int lastSpecialAttack = hp;
private int hpToLoseForSpecialAttack = 5;

//…

if (hp == 0)
{
    //die
}
else if (hp < lastSpecialAttack - hpToLoseForSpecialAttack)
{
    lastSpecialAttack = hp;
    //OR depending how you want it
    lastSpecialAttack -= hpToLoseForSpecialAttack;
    
    //trigger special attack
}
loldertroll loldertroll

2019/5/25

#
Thanks that helped.
danpost danpost

2019/5/25

#
More precise might be the following for when boss takes damage:
int chp = hp;
hp--;
if ((chp-1)%5 > hp%5) specialAttack();
You need to login to post a reply.