top of page

// instance fields
private Actor valueDisplay = new SimpleActor(); // reference to actor
private int value; // control field

​

// in constructor
updateValueDisplay(); // initialize image of actor
addObject(valueDisplay, 50, 15); // add actor to world

​

// method to update actor image
private void updateValueDisplay()
{
    GreenfootImage image;
// for new image to set to 'valueDisplay'
    /* code to create image goes here */
    valueDisplay.setImage(image);
}

​

// method to change control value
public void adjustValue(int amount)
{
    value += amount;
   
// possible code for limiting the value
    {
        if (value < 0) value = 0;
        if (value > maximumValue) value = maximumValue;
    }
    updateValueDisplay();
}

USING A BASIC ACTOR OBJECT (sample World subclass coding)

bottom of page