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

2024/3/8

Points System

Kookaburra737 Kookaburra737

2024/3/8

#
Hey everyone, I was making a game where a lizard collects fire balls to gain heat and has to avoid a car that bounces, but I'm having a lot of trouble displaying the score, would anybody be able to help me out? this is my code for my counter.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class counter 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 void Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }

    /**
     * Act - do whatever the counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }

    }
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
     public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
     private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("Score-Holder.png" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }

    private void Scale()
    {
        GreenfootImage img = new GreenfootImage("Score-Holder.png");
        img.scale(img.getWidth()-150, img.getHeight()-50);
        setImage(img);
    }
}
I just cant figure out how to get the variable from another class to be displayed in this actor... Can someone plz help me?
danpost danpost

2024/3/9

#
Kookaburra737 wrote...
Hey everyone, I was making a game where a lizard collects fire balls to gain heat and has to avoid a car that bounces, but I'm having a lot of trouble displaying the score, would anybody be able to help me out? this is my code for my counter.class: << Code Omitted >> I just cant figure out how to get the variable from another class to be displayed in this actor... Can someone plz help me?
Line 19 should be;
public counter()
and line 64 should probably be:
GreenfootImage text = new GreenfootImage("Score:  " + value, 22, Color.BLACK, transparent);
Other issues may be found in the "(an)other class".
You need to login to post a reply.