hi,
so i am working on the little crab scenario and i want to give the crab five lives. one life is taken away each time the crab is eaten by a lobster.
is this the correct code for the life counter?
if no, then what do i need to change?
if yes, then how do i get it to work?
thanks
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * A simple counter with graphical representation as an actor on screen. * * @author mik * @version 1.0 */ public class LifeCounter extends Actor { private static final Color transparent = new Color( 0 , 0 , 0 , 0 ); private GreenfootImage background; private int value; private int target; /** * Create a new counter, initialised to 0. */ public LifeCounter() { background = getImage(); // get image from class value = 5 ; target = 5 ; updateImage(); } /** * Animate the display to count up (or down) to the current target value. */ public void act() { if (value > target) { value--; updateImage(); } else if (value < target) { value++; updateImage(); } } /** * Add a new score to the current counter value. */ public void add( int score) { target -= score; } /** * Return the current counter value. */ public int getValue() { return value; } /** * Set a new counter value. */ public void setValue( int newValue) { target = newValue; value = newValue; updateImage(); } /** * Update the image on screen to show the current value. */ private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage( "" + value, 22 , Color.BLACK, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/ 2 , (image.getHeight()-text.getHeight())/ 2 ); setImage(image); } } |