is there anything that can help me?
I need a code so that I have a life of an actor in the form of images, then attack if the actor lives in the form of the image will be reduced.
thank you.
import greenfoot.*; public class Lives extends Actor { private int maxLives; // holds the maximum lives allowed private int lifeCount; // holds current number of lives remaining private int height; // holds the height of the image (frame-height) // @param lives is initial and maximum number of lives // @param high is the height of the image (frame-height) public Lives(int lives, int high) { lifeCount = lives; // save number of lives starting with maxLives = lifeCount; // save the maximum number of lives allowed height = high; // save the image height updateImage(); // create the image to display } // call this method when losing a life public boolean loseALife() { if (lifeCount == 0) return true; // lives cannot be negative lifeCount--; // decrement lives remaining updateImage(); // create the new image to display return lifeCount == 0; // return game over flag } // call this method when gaining a life public void gainALife() { if (lifeCount == maxLives) return; // maximum lives set to five lifeCount++; // increment lives remaining updateImage(); // create the new image to display } // call this method to get the number of lives remaining public int getLifeCount() { return lifeCount; } // internal method to create the image to display private void updateImage() { int buffer = height/8; GreenfootImage image = new GreenfootImage(maxLives*height+(maxLives+1)*buffer, height); // create the base image image.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1); // add a frame to base image GreenfootImage life = new GreenfootImage("herz.png"); // get image of heart life.scale(3*height/4, 3*height/4); // resize heart image for (int i=0; i<lifeCount; i++) image.drawImage(life, height/8+buffer+i*9*height/8, buffer); // add hearts to base image setImage(image); // set the image of this object } }
((Lives)getObjects(Lives.class).get(0)).gainALife(); // or if (((Lives)getObjects(Lives.class).get(0)).loseALife()) gameOver(); // or Lives lives = (Lives)getObjects(Lives.class).get(0); if (lives.getLifeCount() == 0) gameOver();
Lives lives = (Lives)getWorld().getObjects(Lives.class).get(0);
int maximumLifeCount = 5; int lifeImageHeight = 30; addObject(new Lives(maximumLifeCount lifeImageHeight), /* x, y */);