Class Counter
java.lang.Object
greenfoot.Actor
Counter
public class Counter
extends greenfoot.Actor
A Counter class that allows you to display a numerical value on screen.
The Counter is an actor, so you will need to create it, and then add it to
the world in Greenfoot. If you keep a reference to the Counter then you
can adjust its value. Here's an example of a world class that
displays a counter with the number of act cycles that have occurred:
class CountingWorld { private Counter actCounter; public CountingWorld() { super(600, 400, 1); actCounter = new Counter("Act Cycles: "); addObject(actCounter, 100, 100); } public void act() { actCounter.setValue(actCounter.getValue() + 1); } }
- Version:
- 1.0
- Author:
- Neil Brown and Michael Kölling
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
act()
Animate the display to count up (or down) to the current target value.void
add
(int score) Add a new score to the current counter value.int
getValue()
Return the current counter value.void
Sets a text prefix that should be displayed before the counter value (e.g.void
setValue
(int newValue) Set a new counter value.Methods inherited from class greenfoot.Actor
addedToWorld, getImage, getIntersectingObjects, getNeighbours, getObjectsAtOffset, getObjectsInRange, getOneIntersectingObject, getOneObjectAtOffset, getRotation, getWorld, getWorldOfType, getX, getY, intersects, isAtEdge, isTouching, move, removeTouching, setImage, setImage, setLocation, setRotation, sleepFor, turn, turnTowards
-
Constructor Details
-
Counter
public Counter() -
Counter
Create a new counter, initialised to 0.
-
-
Method Details
-
act
public void act()Animate the display to count up (or down) to the current target value.- Overrides:
act
in classgreenfoot.Actor
-
add
public void add(int score) Add a new score to the current counter value. This will animate the counter over consecutive frames until it reaches the new value. -
getValue
public int getValue()Return the current counter value. -
setValue
public void setValue(int newValue) Set a new counter value. This will not animate the counter. -
setPrefix
Sets a text prefix that should be displayed before the counter value (e.g. "Score: ").
-