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

2019/5/15

Boss Health Help

bboyguda69 bboyguda69

2019/5/15

#
Hi i'm trying to program a boss fight where the boss will die once you shoot it 10 times. I'm testing my code but the boss never dies even if i set the amount of hits it takes before dying to 1. Heres my Boss class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Boss here. * * @author (your name) * @version (a version number or a date) */ public class BossEnemy extends Actor { private boolean down = true; private int minShotDelay = 40; private int maxShotDelay = 160; private int shotTimer = minShotDelay; public void act() { if(getY() >= getWorld().getHeight()) { down = false; } else if(getY() <= 0) { down = true; } if(down) { setLocation(getX(), getY()+4); } else { setLocation (getX(), getY()-4); } shootRandomly(); if (shotTimer == 0) { getWorld().addObject(new Fireball(), getX(), getY()); shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay); } else shotTimer--; Health(); } public void shootRandomly() { if (shotTimer == 0) { getWorld().addObject(new Fireball(), getX(), getY()); shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay); } else shotTimer--; } public void Health(){ Actor a = getOneIntersectingObject(Bullet.class); if(a != null) { MyWorld5 myWorld5 =(MyWorld5) getWorld(); BossHealth bosshealth = myWorld5.getBossHealth(); bosshealth.addHit(); } } public void Death(){ MyWorld5 myWorld5 =(MyWorld5) getWorld(); BossHealth bosshealth = myWorld5.getBossHealth(); bosshealth.act(); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Boss here. * * @author (your name) * @version (a version number or a date) */ public class BossEnemy extends Actor { private boolean down = true; private int minShotDelay = 40; private int maxShotDelay = 160; private int shotTimer = minShotDelay; public void act() { if(getY() >= getWorld().getHeight()) { down = false; } else if(getY() <= 0) { down = true; } if(down) { setLocation(getX(), getY()+4); } else { setLocation (getX(), getY()-4); } shootRandomly(); if (shotTimer == 0) { getWorld().addObject(new Fireball(), getX(), getY()); shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay); } else shotTimer--; Health(); } public void shootRandomly() { if (shotTimer == 0) { getWorld().addObject(new Fireball(), getX(), getY()); shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay); } else shotTimer--; } public void Health(){ Actor a = getOneIntersectingObject(Bullet.class); if(a != null) { MyWorld5 myWorld5 =(MyWorld5) getWorld(); BossHealth bosshealth = myWorld5.getBossHealth(); bosshealth.addHit(); } } public void Death(){ getWorld().removeObject(this); } }
bboyguda69 bboyguda69

2019/5/15

#
Heres my BossHealth class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class BossHealth here. * * @author (your name) * @version (a version number or a date) */ public class BossHealth extends Actor { int hit = 0; public void BossHealth() { setImage(new GreenfootImage(" " + hit, 26, new Color(255, 0, 102), new Color(0, 0, 0, 0))); } public void act() { if (hit == 1){ MyWorld5 myWorld5 =(MyWorld5) getWorld(); BossEnemy bossenemy = myWorld5.getBossEnemy(); getWorld().removeObject(bossenemy); } } public void addHit() { hit++; } }
bboyguda69 bboyguda69

2019/5/15

#
Any help would be much appreciated, THANK YOU!
danpost danpost

2019/5/15

#
It may be a MyWorld5 class issue (show codes). Of note, I do not see where your BossHealth image is updated when a hit occurs.
You need to login to post a reply.