Package greenfoot

Class Actor

java.lang.Object
greenfoot.Actor

public abstract class Actor extends Object
An Actor is an object that exists in the Greenfoot world. Every Actor has a location in the world, and an appearance (that is: an icon).

An Actor is not normally instantiated, but instead used as a superclass to more specific objects in the world. Every object that is intended to appear in the world must extend Actor. Subclasses can then define their own appearance and behaviour.

One of the most important aspects of this class is the 'act' method. This method is called when the 'Act' or 'Run' buttons are activated in the Greenfoot interface. The method here is empty, and subclasses normally provide their own implementations.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct an Actor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    act()
    The act method is called by the greenfoot framework to give actors a chance to perform some action.
    protected void
    This method is called by the Greenfoot system when this actor has been inserted into the world.
    Returns the image used to represent this actor.
    protected <A> List<A>
    Return all the objects that intersect this object.
    protected <A> List<A>
    getNeighbours(int distance, boolean diagonal, Class<A> cls)
    Return the neighbours to this object within a given distance.
    protected <A> List<A>
    getObjectsAtOffset(int dx, int dy, Class<A> cls)
    Return all objects that intersect the center of the given location (relative to this object's location).
    protected <A> List<A>
    getObjectsInRange(int radius, Class<A> cls)
    Return all objects within range 'radius' around this object.
    protected Actor
    Return an object that intersects this object.
    protected Actor
    getOneObjectAtOffset(int dx, int dy, Class<?> cls)
    Return one object that is located at the specified cell (relative to this objects location).
    int
    Return the current rotation of this actor.
    Return the world that this actor lives in.
    <W> W
    getWorldOfType(Class<W> worldClass)
    Return the world that this actor lives in, provided that it is an instance of the given "worldClass" class (i.e.
    int
    Return the x-coordinate of the actor's current location.
    int
    Return the y-coordinate of the object's current location.
    protected boolean
    Check whether this object intersects with another given object.
    boolean
    Detect whether the actor has reached the edge of the world.
    protected boolean
    isTouching(Class<?> cls)
    Checks whether this actor is touching any other objects of the given class.
    void
    move(int distance)
    Move this actor the specified distance in the direction it is currently facing.
    protected void
    Removes one object of the given class that this actor is currently touching (if any exist).
    void
    Set the image for this actor to the specified image.
    void
    setImage(String filename)
    Set an image for this actor from an image file.
    void
    setLocation(int x, int y)
    Assign a new location for this actor.
    void
    setRotation(int rotation)
    Set the rotation of this actor.
    void
    sleepFor(int sleepFor)
    Sets this actor to sleep for the given number of cycles.
    void
    turn(int amount)
    Turn this actor by the specified amount (in degrees).
    void
    turnTowards(int x, int y)
    Turn this actor to face towards a certain location.

    Methods inherited from class java.lang.Object

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

    • Actor

      public Actor()
      Construct an Actor. The object will have a default image.
  • Method Details

    • act

      public void act()
      The act method is called by the greenfoot framework to give actors a chance to perform some action. At each action step in the environment, each object's act method is invoked, in unspecified order.

      The default implementation does nothing. This method should be overridden in subclasses to implement an actor's action.

    • getX

      public int getX() throws IllegalStateException
      Return the x-coordinate of the actor's current location. The value returned is the horizontal index of the actor's cell in the world.
      Returns:
      The x-coordinate of the object's current location.
      Throws:
      IllegalStateException - If the actor has not been added into a world.
    • getY

      public int getY()
      Return the y-coordinate of the object's current location. The value returned is the vertical index of the actor's cell in the world.
      Returns:
      The y-coordinate of the actor's current location
      Throws:
      IllegalStateException - If the actor has not been added into a world.
    • getRotation

      public int getRotation()
      Return the current rotation of this actor. Rotation is expressed as a degree value, range (0..359). Zero degrees is towards the east (right-hand side of the world), and the angle increases clockwise.
      Returns:
      The rotation in degrees.
      See Also:
    • setRotation

      public void setRotation(int rotation)
      Set the rotation of this actor. Rotation is expressed as a degree value, range (0..359). Zero degrees is to the east (right-hand side of the world), and the angle increases clockwise.
      Parameters:
      rotation - The rotation in degrees.
      See Also:
    • turnTowards

      public void turnTowards(int x, int y)
      Turn this actor to face towards a certain location.
      Parameters:
      x - The x-coordinate of the cell to turn towards
      y - The y-coordinate of the cell to turn towards
    • isAtEdge

      public boolean isAtEdge()
      Detect whether the actor has reached the edge of the world. The actor is at the edge of the world if their position is at, or beyond, the cells at the very edge of the world. For example, if your world is 640 by 480 pixels, an actor is at the edge if its X position is <= 0 or >= 639, or its Y position is <= 0 or >= 479.
      Returns:
      True if the actor is at or beyond the edge cell of the world, and false otherwise.
    • setLocation

      public void setLocation(int x, int y)
      Assign a new location for this actor. This moves the actor to the specified location. The location is specified as the coordinates of a cell in the world.

      If this method is overridden it is important to call this method as "super.setLocation(x,y)" from the overriding method, to avoid infinite recursion.

      Parameters:
      x - Location index on the x-axis
      y - Location index on the y-axis
      See Also:
    • move

      public void move(int distance)
      Move this actor the specified distance in the direction it is currently facing.

      The direction can be set using the setRotation(int) method.

      Parameters:
      distance - The distance to move (in cell-size units); a negative value will move backwards
      See Also:
    • turn

      public void turn(int amount)
      Turn this actor by the specified amount (in degrees).
      Parameters:
      amount - the number of degrees to turn; positive values turn clockwise
      See Also:
    • sleepFor

      public void sleepFor(int sleepFor)
      Sets this actor to sleep for the given number of cycles.

      A sleeping actor will not have act() called it on by the Greenfoot system as usual. So if you call sleepFor(1) then it will skip the next single act() call. If you call sleepFor(3) then it will skip the next three, and so on.

      The actor can still be collided with, will still show in the world, and you can still manually call methods on it (including act(), if you wish, or sleepFor() if you wish to increase or cancel the sleep).

      If you call sleepFor() again, the latest value will replace the current one. So you can wake an actor up by calling sleepFor(0). If you use a negative number such as -1, the actor will sleep indefinitely (unless sleepFor() is called again to replace the value).

      Parameters:
      sleepFor - The number of future act cycles to skip acting, 0 to stop sleeping, or a negative number to sleep indefinitely.
      Since:
      Greenfoot 3.8.1
    • getWorld

      public World getWorld()
      Return the world that this actor lives in.
      Returns:
      The world, or null if this actor is not in a world.
    • getWorldOfType

      public <W> W getWorldOfType(Class<W> worldClass)
      Return the world that this actor lives in, provided that it is an instance of the given "worldClass" class (i.e. that it is an instance of worldClass or one of its subclasses).
      Type Parameters:
      W - The type of the world.
      Parameters:
      worldClass - The class of the world type.
      Returns:
      The world this actor is in, or null if either this actor is not in a world
      Throws:
      ClassCastException - If the actor is in a world, but not one that is an instance of worldClass or one of its subclasses
    • addedToWorld

      protected void addedToWorld(World world)
      This method is called by the Greenfoot system when this actor has been inserted into the world. This method can be overridden to implement custom behaviour when the actor is inserted into the world.

      The default implementation does nothing.

      Parameters:
      world - The world the object was added to.
    • getImage

      public GreenfootImage getImage()
      Returns the image used to represent this actor. This image can be modified to change the actor's appearance.
      Returns:
      The object's image.
    • setImage

      public void setImage(String filename) throws IllegalArgumentException
      Set an image for this actor from an image file. The file may be in jpeg, gif or png format. The file should be located in the project directory.
      Parameters:
      filename - The name of the image file.
      Throws:
      IllegalArgumentException - If the image can not be loaded.
    • setImage

      public void setImage(GreenfootImage image)
      Set the image for this actor to the specified image.
      Parameters:
      image - The image.
      See Also:
    • intersects

      protected boolean intersects(Actor other)
      Check whether this object intersects with another given object.
      Parameters:
      other - The second object to detect the existing of intersection with it.
      Returns:
      True if the object's intersect, false otherwise.
    • getNeighbours

      protected <A> List<A> getNeighbours(int distance, boolean diagonal, Class<A> cls)
      Return the neighbours to this object within a given distance. This method considers only logical location, ignoring extent of the image. Thus, it is most useful in scenarios where objects are contained in a single cell.

      All cells that can be reached in the number of steps given in 'distance' from this object are considered. Steps may be only in the four main directions, or may include diagonal steps, depending on the 'diagonal' parameter. Thus, a distance/diagonal specification of (1,false) will inspect four cells, (1,true) will inspect eight cells.

      Type Parameters:
      A - The class of the object to look for.
      Parameters:
      distance - Distance (in cells) in which to look for other objects.
      diagonal - If true, include diagonal steps.
      cls - Class of objects to look for (passing 'null' will find all objects).
      Returns:
      A list of all neighbours found.
    • getObjectsAtOffset

      protected <A> List<A> getObjectsAtOffset(int dx, int dy, Class<A> cls)
      Return all objects that intersect the center of the given location (relative to this object's location).
      Type Parameters:
      A - The class of the object to look for.
      Parameters:
      dx - X-coordinate relative to this objects location.
      dy - y-coordinate relative to this objects location.
      cls - Class of objects to look for (passing 'null' will find all objects).
      Returns:
      List of objects at the given offset. The list will include this object, if the offset is zero.
    • getOneObjectAtOffset

      protected Actor getOneObjectAtOffset(int dx, int dy, Class<?> cls)
      Return one object that is located at the specified cell (relative to this objects location). Objects found can be restricted to a specific class (and its subclasses) by supplying the 'cls' parameter. If more than one object of the specified class resides at that location, one of them will be chosen and returned.
      Parameters:
      dx - X-coordinate relative to this objects location.
      dy - y-coordinate relative to this objects location.
      cls - Class of objects to look for (passing 'null' will find all objects).
      Returns:
      An object at the given location, or null if none found.
    • getObjectsInRange

      protected <A> List<A> getObjectsInRange(int radius, Class<A> cls)
      Return all objects within range 'radius' around this object. An object is within range if the distance between its centre and this object's centre is less than or equal to 'radius'.
      Type Parameters:
      A - The class of the object to look for.
      Parameters:
      radius - Radius of the circle (in cells)
      cls - Class of objects to look for (passing 'null' will find all objects).
      Returns:
      List of objects of the given class type within the given radius.
    • getIntersectingObjects

      protected <A> List<A> getIntersectingObjects(Class<A> cls)
      Return all the objects that intersect this object. This takes the graphical extent of objects into consideration.
      Type Parameters:
      A - The class of the object to look for.
      Parameters:
      cls - Class of objects to look for (passing 'null' will find all objects).
      Returns:
      List of objects of the given class type that intersect with the current object.
    • getOneIntersectingObject

      protected Actor getOneIntersectingObject(Class<?> cls)
      Return an object that intersects this object. This takes the graphical extent of objects into consideration.
      Parameters:
      cls - Class of objects to look for (passing 'null' will find all objects).
      Returns:
      An object of the given class type that intersects with the current object.
    • isTouching

      protected boolean isTouching(Class<?> cls)
      Checks whether this actor is touching any other objects of the given class.
      Parameters:
      cls - Class of objects to look for (passing 'null' will check for all actors).
      Returns:
      True if there is an object of the given class type that intersects with the current object, false otherwise.
    • removeTouching

      protected void removeTouching(Class<?> cls)
      Removes one object of the given class that this actor is currently touching (if any exist).
      Parameters:
      cls - Class of objects to remove (passing 'null' will remove any actor).