|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgreenfoot.Actor
public abstract class Actor
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 | |
---|---|
Actor()
Construct an Actor. |
Method Summary | |
---|---|
void |
act()
The act method is called by the greenfoot framework to give actors a chance to perform some action. |
protected void |
addedToWorld(World world)
This method is called by the Greenfoot system when this actor has been inserted into the world. |
GreenfootImage |
getImage()
Returns the image used to represent this actor. |
protected java.util.List |
getIntersectingObjects(java.lang.Class cls)
Return all the objects that intersect this object. |
protected java.util.List |
getNeighbours(int distance,
boolean diagonal,
java.lang.Class cls)
Return the neighbours to this object within a given distance. |
protected java.util.List |
getObjectsAtOffset(int dx,
int dy,
java.lang.Class cls)
Return all objects that intersect the center of the given location (relative to this object's location). |
protected java.util.List |
getObjectsInRange(int radius,
java.lang.Class cls)
Return all objects within range 'radius' around this object. |
protected Actor |
getOneIntersectingObject(java.lang.Class cls)
Return an object that intersects this object. |
protected Actor |
getOneObjectAtOffset(int dx,
int dy,
java.lang.Class cls)
Return one object that is located at the specified cell (relative to this objects location). |
int |
getRotation()
Return the current rotation of this actor. |
World |
getWorld()
Return the world that this actor lives in. |
int |
getX()
Return the x-coordinate of the actor's current location. |
int |
getY()
Return the y-coordinate of the object's current location. |
protected boolean |
intersects(Actor other)
Check whether this object intersects with another given object. |
void |
move(int distance)
Move this actor the specified distance in the direction it is currently facing. |
void |
setImage(GreenfootImage image)
Set the image for this actor to the specified image. |
void |
setImage(java.lang.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 |
turn(int amount)
Turn this actor by the specified amount (in degrees). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Actor()
Method Detail |
---|
public void act()
The default implementation does nothing. This method should be overridden in subclasses to implement an actor's action.
public int getX() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- If the actor has not been added into a world.public int getY()
java.lang.IllegalStateException
- If the actor has not been added into a world.public int getRotation()
setRotation(int)
public void setRotation(int rotation)
rotation
- The rotation in degrees.turn(int)
public void setLocation(int x, int y)
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.
x
- Location index on the x-axisy
- Location index on the y-axismove(int)
public void move(int distance)
The direction can be set using the setRotation(int)
method.
distance
- The distance to move (in cell-size units); a negative value
will move backwardssetLocation(int, int)
public void turn(int amount)
amount
- the number of degrees to turn; positive values turn clockwisesetRotation(int)
public World getWorld()
protected void addedToWorld(World world)
The default implementation does nothing.
world
- The world the object was added to.public GreenfootImage getImage()
public void setImage(java.lang.String filename) throws java.lang.IllegalArgumentException
filename
- The name of the image file.
java.lang.IllegalArgumentException
- If the image can not be loaded.public void setImage(GreenfootImage image)
image
- The image.setImage(String)
protected boolean intersects(Actor other)
protected java.util.List getNeighbours(int distance, boolean diagonal, java.lang.Class cls)
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.
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).
protected java.util.List getObjectsAtOffset(int dx, int dy, java.lang.Class cls)
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).
protected Actor getOneObjectAtOffset(int dx, int dy, java.lang.Class cls)
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).
protected java.util.List getObjectsInRange(int radius, java.lang.Class cls)
radius
- Radius of the circle (in cells)cls
- Class of objects to look for (passing 'null' will find all objects).protected java.util.List getIntersectingObjects(java.lang.Class cls)
cls
- Class of objects to look for (passing 'null' will find all objects).protected Actor getOneIntersectingObject(java.lang.Class cls)
cls
- Class of objects to look for (passing 'null' will find all objects).
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |