top of page

As inferred previously in this tutorial, using an Actor object to display text that changes is the way to go.  By keeping a reference to this actor in the World subclass, we can change its image when needed.  A simple Actor subclass can be used to help illustrate this.  The following line of code is the entire class code that basically allows an Actor object to be created from the Actor class (which cannot be done directly because it is defined as an abstract class):

​

public class SimpleActor extends greenfoot.Actor {}


Then, you can have a reference to an Actor object:

​

private Actor actor = new SimpleActor();


with a method to change its image:

​

    private void updateActorImage(String text)
    {
        GreenfootImage image = new GreenfootImage(text, size, null, null);
        actor.setImage(image);
    }


The name of the field and method can describe what the Actor object and method are for.  As an example, they could be a 'scoreDisplay' field and a 'updateScoreDisplay' method that has the value of 'int score' passed to it (or the String representation of it).

Normally, the image displayed would represent a specific state that is stored in a field.  For examples, the score value (as in the above example) could be saved in a 'int' field; any state involving an 'int' value could be stored in a field (a level number, a score, a timer, or any number of other states).  Then, the value of the field would be used to determine what text to be used in the image:

​

// with fields:  private int score;

//               private Actor scoreDisplay;
String text = "Score: "+score;
GreenfootImage image = new GreenfootImage(text, size, null null);
scoreDisplay.setImage(image);

​

// with fields:  private boolean running;

//               private Actor runningDisplay;
String text = running? "Running" : "Idle";
GreenfootImage image = new GreenfootImage(text, size, null, null);
runningDisplay.setImage(image);

 

// with fields: private String[] phases;

//              private int phase;

//              private Actor phaseDisplay;
String text = phases[phase];
GreenfootImage image = new GreenfootImage(text, size, null, null);
phaseDisplay.setImage(image);


We can have the 'int' value converted to display it in different ways.  For example, if you were to be counting act cycles, or frames, using an 'int' field, you could convert it to display something similar to a time format, "mm:ss", using something like the following:

​

// fields: private int frames;

//         private Actor timeDisplay;
int time = frames/55; // normal frames to seconds
String minutes = "0"+(time/60);
String seconds = "0"+(time%60);
String mins = minutes.substring(minutes.length()-2);
String secs = seconds.substring(seconds.length()-2);
String text = mins+":"+secs;
GreenfootImage image = new GreenfootImage(text, size, null, null);
timeDisplay.setImage(image);


At this point, we have seen how a simple Actor object can be used to display a state in the form of an image with textual content.  It should be noted that if you are to be creating more than a few objects of a specific type, like counters, then creating a specialized class may help to reduce redundant code.  By using a visual representation of the value, a simple Actor object can be used to display things like a bar where the length of its colored portion represents a percentage of the maximum value of the field (like for a 'health' or 'progress' bar) or a row of images where each image represents one unit of value (like for number of 'lives' remaining).

​

A BASIC CODING SYSTEM

A simple format that can be used for all types of variable displays follows.

The basic ingredients are:


(1) a reference to an Actor object in the world that is used to display the image;
(2) a field to hold a variable that will control what value the actor displays;
(3) a method to update the image of the actor;
(4) a method to change the value of the control variable which calls the method in (3);

​

The methods in (3) and (4) can often be combined into one method.  Sample coding can be viewed via the following links:

​

    Sample coding of basic display actor in a World subclass


    Sample coding of basic display actor in an Actor subclass

USING A BASIC ACTOR OBJECT

bottom of page