I'm trying to make a health bar with 180 separate sprites to make up 180 total health. I'm trying to do this with setImage. How would I do this? (or is there a more efficient way?) Thanks.


1 | setImage( "Health" +(health+ 1 )+ ".png" ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public void act() { takeDamage(); } public void takeDamage() { if (lives == 9 ) { setImage( "Health9.png" ); } if (lives == 8 ) { setImage( "Health8.png" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class Boss extends Actor { private int health = 180 ; private BossHealth bossHealth = new BossHealth(); // creates the health bar for this boss protected void addedToWorld(World world) { positionHealthbar(); // adds bar into world } /** adds healthbar to world, if needed, and updates its position */ private void positionHealthbar() { getWorld().addObject(bossHealth, 0 , 0 ); bossHealth.setLocation(getX(), getY()- 30 ); } /** class of healthbar */ private class BossHealth extends Actor { public BossHealth() { updateImage(); // initializes image } /** updates image of healthbar */ protected void updateImage() { setImage( new GreenfootImage( "Health" +health+ ".png" )); } } } |
1 2 3 4 5 6 7 8 | health--; if (health == 0 ) { getWorld().removeObject(bossHealth); getWorld().removeObject( this ); return ; } bossHealth().updateImage(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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 Boss extends obstacles { private int health = 63 ; private BossHealth bossHealth = new BossHealth(); public int BossX; public int BossY; protected void addedToWorld(World bossworld) { positionHealthBar(); } /** * Act - do whatever the Boss wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Plane2 plane2 = (Plane2)getWorld().getObjects(Plane2. class ).get( 0 ); turnTowards(plane2.getX(), plane2.getY()); Boss boss = new Boss(); move( 6 ); BossX = getX(); BossY = getY(); if (isTouching(Bomb. class )) { health--; } if (health == 0 ) { getWorld().removeObject( this ); return ; } } private void positionHealthBar() { getWorld().addObject(bossHealth, 0 , 0 ); bossHealth.setLocation(getX(), getY()- 30 ); } private class BossHealth extends Actor { public BossHealth() { updateImage(); // initializes image } /** updates image of healthbar */ protected void updateImage() { setImage( new GreenfootImage( "Health" +health+ ".png" )); } } } |
1 | positionHealthBar(); |
1 | getWorld().removeObject(bossHealth); |
1 | bossHealth.updateImage(); |