Class 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

      Constructors 
      Constructor Description
      Counter()  
      Counter​(java.lang.String prefix)
      Create a new counter, initialised to 0.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void 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 setPrefix​(java.lang.String prefix)
      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, turn, turnTowards
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Counter

        public Counter()
      • Counter

        public Counter​(java.lang.String prefix)
        Create a new counter, initialised to 0.
    • Method Detail

      • act

        public void act()
        Animate the display to count up (or down) to the current target value.
        Overrides:
        act in class greenfoot.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

        public void setPrefix​(java.lang.String prefix)
        Sets a text prefix that should be displayed before the counter value (e.g. "Score: ").