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

2019/3/26

The healthbar isn't taking damage from my Minions???

KennyBoy KennyBoy

2019/3/26

#
I typed the code in exactly how Jim Stewart did it but my "Minions" aren't causing damage to the healthbar public class Goku extends Animal { public int gokuRotation; boolean touchingMinion = false; public static int gokuX, gokuY; /** * Act - do whatever the Goku wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(2); shoot(); gokuRotation = getRotation(); checkKeys(); gokuX = getX(); gokuY = getY(); } public void checkKeys() { if (Greenfoot.isKeyDown("a")) { turn(-5); } if (Greenfoot.isKeyDown("d")) { turn(5); } } public void shoot() { if(Greenfoot.getMouseInfo() != null) { if(Greenfoot.getMouseInfo().getButton() == 1) getWorld() .addObject(new Bullet(gokuRotation), getX(), getY()); } } public boolean isTouching() { return getOneIntersectingObject(Minion.class) != null; } public void hitMinion() { Actor minion = getOneIntersectingObject(Minion.class); if(minion !=null) { World myWorld = getWorld(); Cave1 cave1 = (Cave1)myWorld; HealthBar healthbar = cave1.getHealthBar(); if(touchingMinion == false) { healthbar.loseHealth(); touchingMinion = true; if(healthbar.health <=0) { GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); myWorld.removeObject(this); } } } else { touchingMinion = false; } } public class HealthBar extends Actor { int health = 4; int healthBarWidth = 80; int healthBarHeight = 15; int pixelsPerHealthPoint = (int)healthBarWidth/health; /** * Act - do whatever the HealthBar wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { update(); } public void HealthBar() { update(); } public void update() { setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight +2)); GreenfootImage myImage = getImage(); myImage.setColor(Color.WHITE); myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1); myImage.setColor(Color.RED); myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight); } public void loseHealth() { health--; } }
danpost danpost

2019/3/26

#
The problem is most probably in your Cave1 class.
You need to login to post a reply.