This site requires JavaScript, please enable it in your browser!
Greenfoot back
RobiJefferson
RobiJefferson wrote ...

2013/5/13

Code Help

RobiJefferson RobiJefferson

2013/5/13

#
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.
danpost danpost

2013/5/13

#
What are you looking for? a row of hearts? a status bar?
RobiJefferson RobiJefferson

2013/5/13

#
yes, that's what I was looking for
danpost danpost

2013/5/13

#
A simple (though somewhat lengthy) class for what you are looking for could be the following. This class creates an image that displays a framed area containing any number of hearts, each one representing a life for the character that the user controls. You will need to set the image of this class to "herz.png" located in the 'symbols' category. This is just to copy the image file into the images folder of your scenario. Afterwards, you can reset the image of this class to 'No image' (if you wish).
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
    }
}
To work with this class, from the world:
((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();
From an actor class:
Lives lives = (Lives)getWorld().getObjects(Lives.class).get(0);
Create a Lives object in the world constructor (or method it calls) with
int maximumLifeCount = 5;
int lifeImageHeight = 30;
addObject(new Lives(maximumLifeCount lifeImageHeight), /* x, y */);
You need to login to post a reply.