I have a health bar that I need to change based on when projectiles hit the player. The health bar I got with the help of greenfoot member so its not my personal code so I am only mostly sure I understand how it works.
I want the Ship( player ) to lose health every time a MeanMissile ( enemy projectile) makes contact.
Ship Code-
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 | import greenfoot.*; import java.awt.Color; import java.awt.color.*; public class HealthBar extends Actor { private final int maxValue = 200 ; // whatever max value you desire private int barValue = 200 ; private int lastValue = 0 ; public HealthBar() { } public void act() { if (barValue != lastValue) { lastValue = barValue; int pctHealth = ( int ) ( 200 * barValue / maxValue); // 200 is maxPixelHealth // Create thermometer -- sorta speak GreenfootImage imgOne = new GreenfootImage( 206 , 25 ); imgOne.setColor(Color.CYAN); imgOne.fill(); imgOne.setColor(Color.BLUE); imgOne.drawRect( 2 , 2 , 202 , 21 ); // Add mercury, if there is any temperature -- sorta speak if (pctHealth != 0 ) { GreenfootImage imgTwo = new GreenfootImage(pctHealth, 19 ); imgTwo.setColor(Color.RED); imgTwo.fill(); // Completes the second image imgOne.drawImage(imgTwo, 3 , 3 ); // Puts mercury into the thermometer } // imgOne.scale(myX, myY); // Dimensions myX and myY are whatever size you wish to make it imgOne.setTransparency( 128 ); // Adjust value as wanted or delete statement for no transparency setImage(imgOne); } } public void chgHealth( int chgValue) { barValue += chgValue; if (barValue > maxValue) barValue = maxValue; if (barValue < 0 ) barValue = 0 ; } public int getHealth() { return barValue; } } |
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 | import greenfoot.*; /** * An demo class which is meant to be a camera follower. * It moves to face your mouse cursor, and it can move * back and forward. * * @author Sven van Nigtevecht * @version 1.0 */ public class Bug extends ScrollActor { /** The number of cells we move forward and backword */ private static final int MOVE_AMOUNT = 5 ; private int shootingCounter; public int angle; public int barValue; /** * Move to face the mouse, * and listen to the up and down keys. */ public void act() { MouseInfo m = Greenfoot.getMouseInfo(); getWorld().setCameraDirection(getRotation()); Actor First = null ; if (Greenfoot.isKeyDown( "down" )) { // move the camera backwards: getWorld().moveCamera(-MOVE_AMOUNT); } if (Greenfoot.isKeyDown( "up" )) { // move the camera forwards: getWorld().moveCamera(MOVE_AMOUNT); } if (Greenfoot.isKeyDown( "left" )) { turn ( - 3 ); angle -= 3 ; } if (Greenfoot.isKeyDown( "right" )) { turn ( 3 ); angle += 3 ; } if (Greenfoot.isKeyDown( "b" )) { getWorld().moveCamera( MOVE_AMOUNT * 2 ) ; } if (Greenfoot.isKeyDown( "b" )) { getWorld().moveCamera( MOVE_AMOUNT * 2 ) ; } if (First != null ) { move (- 10 ); } if ( angle > 360 ) { angle = 0 ; } if ( angle < 0 ) { angle = 360 ; } shoot(); Actor MeanMissile; MeanMissile = getOneObjectAtOffset( 0 , 0 , MeanMissile. class ); if ( MeanMissile != null ) { World Galaxy; Galaxy = getWorld(); Galaxy.removeObject(MeanMissile); Galaxy.addObject( new explosion(), getGlobalX(), getGlobalY()); } } public void shoot() { shootingCounter--; if (Greenfoot.isKeyDown( "s" ) && shootingCounter <= 0 ) { shootingCounter= 20 ; getWorld().addObject( new ShipMissile(angle), getGlobalX(), getGlobalY()); } } public int getGlobalY() { if (world == null ) throw new IllegalStateException( "Actor not in world. Either is hasn't" + " been inserted, or it has been deleted." ); return globalY; } public int getGlobalX() { if (world == null ) throw new IllegalStateException( "Actor not in world. Either is hasn't" + " been inserted, or it has been deleted." ); return globalX; } } |