i have made a class that creates a healthbar for the spawning units, but i have a few problems with it.
a) it doesn't fill in the bar till the unit is hit
b) it doesn't fill it proportionally
trying to get it to show how much they've damaged the unit when the tower hits.
here's the class for the healthbar:
and here's the class i'm implementing it in:
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 | public class healthbar extends Actor { /** * Act - do whatever the healthbar wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ GreenfootImage bar = new GreenfootImage( 50 , 10 ); //width and height public healthbar( int health) { bar.setColor( new Color( 0 , 0 , 255 )); bar.fill(); bar.setColor( new Color( 0 , 255 , 0 )); bar.drawRect( 0 , 0 , 48 , 8 ); bar.fillRect( 0 , 0 , health, 8 ); setImage(bar); } // public void act() // { // // Add your action code here. // //imageUpdate(); // } public void imageUpdate( int health) { bar.clear(); bar.setColor( new Color( 0 , 0 , 255 )); bar.fill(); bar.setColor( new Color( 0 , 255 , 0 )); bar.fillRect( 0 , 0 , health, 8 ); setImage(bar); } } |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | public class enemies extends Actor { /** * Act - do whatever the enemies wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ int x = 0 ; int y = 0 ; private int wave = 1 ; //get from wave class public int health; public int mins; public int lifeLost; private int input = 0 ; private int i = 1 ; private int j = 0 ; private int slow = 0 ; private healthbar HPbar; public enemies() { //getWorld().addObject(new healthbar(health), this.getX(), this.getY() - 10); HPbar = new healthbar(health); } public void act() { // Add your action code here. if (slowStat() && slow < 100 ) { slow++; } else { move(); getWorld().addObject(HPbar, this .getX(), this .getY() - 20 ); HPbar.setLocation( this .getX(), this .getY() - 10 ); if (slow == 100 ) { ((tdpath)getWorld()).removeSlowMatter(); slow = 0 ; } } // slowMove(); checkHealth(); checkWave(); if (((tdpath)getWorld()).waveNum() > 5 && ((tdpath)getWorld()).spawnsDone() && ((tdpath)getWorld()).getSpawnsAlive() == 0 ) { input = 0 ; } if (j == 5 ) { i++; j = 0 ; } } public int getHealth() { return health; } public void decHealth( int damage) { health = health - damage; HPbar.imageUpdate(health); checkHealth(); } public void setHealth( int hp) { health = hp; } public void checkHealth() { if (health <= 0 ) { ((tdpath)getWorld()).mineralsAdd(mins); ((tdpath)getWorld()).killSpawn(); getWorld().removeObject(HPbar); ((tdpath)getWorld()).removeObject( this ); } } public void move() { if (getWorld().getColorAt(getX()+ 1 , getY()).getRed() < 10 && getWorld().getColorAt(getX()+ 1 , getY()).getGreen() < 10 && x !=- 1 ) { setLocation(getX() + 1 ,getY()); x = 1 ; y = 0 ; } else if (getWorld().getColorAt(getX()- 1 , getY()).getRed() < 10 && getWorld().getColorAt(getX()- 1 , getY()).getGreen() < 10 && x != 1 ) { setLocation(getX()- 1 , getY()); x = - 1 ; y = 0 ; } else if (getWorld().getColorAt(getX(), getY()+ 1 ).getRed() < 10 && getWorld().getColorAt(getX(), getY()+ 1 ).getGreen() < 10 && y != - 1 ) { setLocation(getX(), getY()+ 1 ); x = 0 ; y = 1 ; } else if (getWorld().getColorAt(getX(), getY()- 1 ).getRed() < 10 && getWorld().getColorAt(getX(), getY()- 1 ).getGreen() < 10 && y != 1 ) { setLocation(getX(), getY()- 1 ); x = 0 ; y = - 1 ; } } public void slowMove() { } public int getLivesLost() { return lifeLost; } public int getSlow() { return slow; } public void checkWave() { if ((((tdpath)getWorld()).waveNum() > 5 ) && input < 1 ) //&& (((tdpath)getWorld()).waveNum())%5 >= 1 && input < 1) { healthInc(); input++; j++; } } public void healthInc() { setHealth(health*( 2 *(((tdpath)getWorld()).waveNum()/ 5 ))); } public boolean slowStat() { if (!getObjectsInRange( 50 , slowMatter. class ).isEmpty()) { return true ; } return false ; } public void setSlow( int SL) { slow = SL; } } |