I need help on getting my Health Bar to work, I thought everything was going fine. I have this one error that I don't know how to fix. I'm also doing a multiplayer game, so I'm not sure if that affects anything.
It says there's an error with the last two codes, "public HealthBar1..." The rest of my code is fine, but when I try to delete this, then errors show up in my players code.
I need help with this issue. Thanks.
import greenfoot.*; public class MyWorld extends World { HealthBar1 healthbar1 = new HealthBar1(); HealthBar2 healthbar2 = new HealthBar2(); Player1 player1 = new Player1(); Player2 player2 = new Player2(); public MyWorld() { super(600, 400, 1); prepare(); } private void prepare() { addObject(healthbar1, 100, 40); addObject(healthbar2, 500, 40); addObject(player1, 600, 250); addObject(player2, 100, 250); } public HealthBar1.getHealthBar1() { return healthbar1; } public HealthBar2.getHealthBar2() { return healthbar2; } }
import greenfoot.*; public class Player1 extends Players { boolean bulletshot = false; private int bulletTimer = 0; public void act() { setRotation(90); if (Greenfoot.isKeyDown("W")) { move(-4); } if (Greenfoot.isKeyDown("S")) { move(4); } setRotation(0); shoot(); shot(); } public void shoot() { if (bulletTimer < 50) { bulletTimer++; return; } if (Greenfoot.isKeyDown("D")) { Bullets bullets = new Bullets(); getWorld().addObject(bullets, getX(), getY()); bullets.setRotation(getRotation()); bullets.move(5); bulletTimer = 0; } } public void shot() { Actor Bullet = getOneIntersectingObject (Bullets.class); if (Bullet != null) { World myWorld = getWorld(); HealthBar1 healthbar = MyWorld.getHealthBar1(); if (bulletshot == false) { healthbar.loseHealth(); bulletshot = true; if (healthbar.health <=0) { Win2 win2 = new Win2(); MyWorld.addObject(win2, MyWorld.getWidth()/2, MyWorld.getHeight()/2); MyWorld.removeObject(this); } } } else { bulletshot= false; } } }
import greenfoot.*; import greenfoot.Color; public class HealthBar1 extends Player1 { int health = 4; int healthBarWidth = 80; int healthBarHeight = 15; int healthPoint = healthBarWidth/health; public void act() { update(); } public HealthBar1() { 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.GREEN); myImage.fillRect(1, 1, health*healthPoint, healthBarHeight); } public void loseHealth() { health--; update(); } }