|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgreenfoot.sofiainternal.Actor
sofia.graphics.Shape
sofia.graphics.ImageShape
sofia.micro.Actor
public class Actor
An Actor is an object (or character) that exists in a "microworld". Every Actor has a location in the world, and an appearance (that is: an icon, image, or shape).
An Actor is not normally instantiated directly, but instead used as a superclass. Create a subclass to represent 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 behavior.
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 interface. The method here is empty, and subclasses normally provide their own implementations.
Constructor Summary | |
---|---|
Actor()
Create a new Actor. |
|
Actor(boolean scaleToCell)
Create a new Actor. |
|
Actor(java.lang.String nickName)
Create a new Actor. |
|
Actor(java.lang.String nickName,
boolean scaleToCell)
Create a new Actor. |
Method Summary | ||
---|---|---|
void |
act()
The act method is called by the system to give actors a chance to perform some action. |
|
void |
addedToWorld(World world)
This method is called by the system when this actor has been inserted into the world. |
|
void |
addOther(Shape newShape)
Add another shape to the same view (or parent) containing this shape. |
|
int |
getGridX()
Return the x-coordinate of the actor's current location. |
|
int |
getGridY()
Return the y-coordinate of the object's current location. |
|
Image |
getImage()
Returns the image used to represent this actor, as an Image
object. |
|
protected
|
getIntersectingObjects(java.lang.Class<MyActor> cls)
Return all the objects that intersect this object. |
|
protected
|
getNeighbors(float distance,
boolean diagonal,
java.lang.Class<MyActor> cls)
Return the neighbors to this object within a given distance. |
|
java.lang.String |
getNickName()
Get the "nickname" for this object--an assignable name that is used in the toString() representation of this object. |
|
protected
|
getObjectsAtOffset(float dx,
float dy,
java.lang.Class<MyActor> cls)
Return all objects that intersect the center of the given location (relative to this object's location). |
|
protected
|
getObjectsInRange(float radius,
java.lang.Class<MyActor> cls)
Return all objects within range 'radius' around this object. |
|
protected
|
getOneIntersectingObject(java.lang.Class<MyActor> cls)
Return an object that intersects this object. |
|
protected
|
getOneObjectAtOffset(float dx,
float dy,
java.lang.Class<MyActor> cls)
Return one object that is located at the specified cell (relative to this object's location). |
|
Shape |
getShape()
Returns the image used to represent this actor, as a Shape
object. |
|
World |
getWorld()
Return the world that this actor lives in. |
|
void |
move(int distance)
Move this actor the specified distance in the direction it is currently facing. |
|
void |
remove()
Remove this shape from its view (or parent). |
|
void |
setBounds(android.graphics.RectF newBounds)
Sets the bounding rectangle of the shape. |
|
void |
setGridLocation(int x,
int y)
Assign a new location for this actor. |
|
void |
setGridX(int x)
Assign a new horizontal (x-axis) location for this actor. |
|
void |
setGridY(int y)
Assign a new vertical (y-axis) location for this actor. |
|
void |
setImage(Image image)
Set a bitmap as the image for this actor. |
|
void |
setImage(java.lang.String fileName)
Set an image for this actor from an image file. |
|
void |
setNickName(java.lang.String nickName)
Set the "nickname" for this object--an assignable name that is used in the toString() representation of this object. |
|
void |
setPosition(sofia.graphics.PointAndAnchor pointAndAnchor)
Sets the position of the receiver based on the specified point and anchor, leaving its size unchanged. |
|
void |
setPosition(android.graphics.PointF position)
Sets the origin (top-left corner) of the receiver. |
|
void |
setPositionAnchor(sofia.graphics.Anchor anchor)
Set the position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions. |
|
void |
setPositionAnchor(android.graphics.PointF anchor)
Set the position anchor, which is an offset relative to the upper left corner of the shape that is used as the shape's "origin" for the purposes of getting/setting x-y positions. |
|
void |
setRotation(double angleInDegrees)
Set the rotation of this actor. |
|
void |
setX(float x)
Sets the x-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default). |
|
void |
setY(float y)
Sets the y-coordinate of the anchor point of the shape's bounding box (the top-left corner, by default). |
|
java.lang.String |
toString()
Returns a human-readable string representation of the actor. |
|
void |
turn(double amount)
Turn this actor by the specified amount (in degrees). |
|
void |
turnTowards(Actor target)
Turn this actor to face towards another actor (in the same world). |
|
void |
turnTowards(int x,
int y)
Turn this actor to face towards a certain location. |
Methods inherited from class sofia.graphics.ImageShape |
---|
getSourceBounds, setBitmap, setSourceBounds, setSourceBounds |
Methods inherited from class sofia.graphics.Shape |
---|
conditionallyRelayout, conditionallyRepaint, contains, contains, extendsOutside, getAlpha, getBounds, getColor, getHeight, getPosition, getPositionAnchor, getRotation, getRotationPivot, getShapeParent, getWidth, getX, getY, getZIndex, intersects, isInFrontOf, isVisible, move, notifyParentOfPositionChange, onBoundsResolved, rotateBy, setAlpha, setColor, setRotation, setRotation, setVisible, setZIndex |
Methods inherited from class greenfoot.sofiainternal.Actor |
---|
_gf_addToWorld, _gf_getBoundingXs, _gf_getBoundingYs, _gf_getDelegate, _gf_getImage, _gf_getIntersectingObjects, _gf_getNeighbours, _gf_getObjectsAtOffset, _gf_getObjectsInRange, _gf_getOneIntersectingObject, _gf_getOneObjectAtOffset, _gf_getRotation, _gf_getX, _gf_getY, _gf_setImage, _gf_setImage, _gf_setLocation, _gf_setRotation, _gf_turn, addedToWorld, containsPoint, intersects |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Actor()
public Actor(java.lang.String nickName)
nickName
- The nickname for this actor.public Actor(boolean scaleToCell)
scaleToCell
- If true, the Actor's image will be scaled to
the dimensions of a single World grid cell, while
preserving aspect ratio. If false, the image
will be sized relative to the underlying bitmap
or shape.public Actor(java.lang.String nickName, boolean scaleToCell)
nickName
- The nickname for this actor.scaleToCell
- If true, the Actor's image will be scaled to
the dimensions of a single World grid cell, while
preserving aspect ratio. If false, the image
will be sized relative to the underlying bitmap
or shape.Method Detail |
---|
public void act()
The default implementation does nothing. This method should be overridden in subclasses to implement an actor's behavior.
act
in class greenfoot.sofiainternal.Actor
public void move(int distance)
The direction can be set using the setRotation(double)
method.
move
in class greenfoot.sofiainternal.Actor
distance
- The distance to move (in cell-size units). A
negative value will move backwards.setGridLocation(int, int)
public void turn(double amount)
amount
- The number of degrees to turn. Positive values turn
clockwise.setRotation(double)
public void turnTowards(int x, int y)
turnTowards
in class greenfoot.sofiainternal.Actor
x
- The x-coordinate of the cell to turn towards.y
- The y-coordinate of the cell to turn towards.public void turnTowards(Actor target)
target
- The actor to turn towards.public World getWorld()
getWorld
in class greenfoot.sofiainternal.Actor
public Image getImage()
Image
object. This image can be modified to change the actor's appearance.
getImage
in class sofia.graphics.ImageShape
public Shape getShape()
Shape
object. This shape can be modified to change the actor's appearance.
public void setImage(java.lang.String fileName) throws java.lang.IllegalArgumentException
fileName
- The name of the image file.
java.lang.IllegalArgumentException
- If the image cannot be loaded.public void setImage(Image image)
image
- The bitmap to use as this actor's image.protected <MyActor extends Actor> java.util.Set<MyActor> getNeighbors(float distance, boolean diagonal, java.lang.Class<MyActor> 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.
MyActor
- The type of actor to look for, as specified
in the cls parameter.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 <MyActor extends Actor> java.util.Set<MyActor> getObjectsAtOffset(float dx, float dy, java.lang.Class<MyActor> cls)
MyActor
- The type of actor to look for, as specified
in the cls parameter.dx
- X-coordinate relative to this object's location.dy
- Y-coordinate relative to this object's location.cls
- Class of objects to look for (passing 'null' will find all
objects).
protected <MyActor extends Actor> MyActor getOneObjectAtOffset(float dx, float dy, java.lang.Class<MyActor> cls)
MyActor
- The type of actor to look for, as specified
in the cls parameter.dx
- X-coordinate relative to this object's location.dy
- Y-coordinate relative to this object's location.cls
- Class of objects to look for (passing 'null' will find all
objects).
protected <MyActor extends Actor> java.util.Set<MyActor> getObjectsInRange(float radius, java.lang.Class<MyActor> cls)
MyActor
- The type of actor to look for, as specified
in the cls parameter.radius
- Radius of the circle (in cells).cls
- Class of objects to look for (passing 'null' will find
all objects).
protected <MyActor extends Actor> java.util.Set<MyActor> getIntersectingObjects(java.lang.Class<MyActor> cls)
MyActor
- The type of actor to look for, as specified
in the cls parameter.cls
- Class of objects to look for (passing 'null' will find all
objects).
protected <MyActor extends Actor> MyActor getOneIntersectingObject(java.lang.Class<MyActor> cls)
MyActor
- The type of actor to look for, as specified
in the cls parameter.cls
- Class of objects to look for (passing 'null' will find all
objects).
public void addedToWorld(World world)
The default implementation does nothing.
world
- The world the object was added to.public int getGridX()
java.lang.IllegalStateException
- If the actor has not been added into
a world.public void setGridX(int x)
x
- The location to move to on the x-axis.setGridLocation(int, int)
public void setX(float x)
setX
in class Shape
x
- The x-coordinate of the anchor point of the shape's
bounding box.Shape.setPositionAnchor(Anchor)
public int getGridY()
java.lang.IllegalStateException
- If the actor has not been added into
a world.public void setGridY(int y)
y
- The location to move to on the y-axis.setGridLocation(int, int)
public void setY(float y)
setY
in class Shape
y
- The y-coordinate of the anchor point of the shape's
bounding box.Shape.setPositionAnchor(Anchor)
public void setGridLocation(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
- The location to move to on the x-axis.y
- The location to move to on the y-axis.move(int)
public void setPosition(android.graphics.PointF position)
setPosition
in class Shape
position
- A PointF
object describing the origin of the
shape.public void setPosition(sofia.graphics.PointAndAnchor pointAndAnchor)
setPosition
in class Shape
pointAndAnchor
- A PointAndAnchor
object describing the
position of the shape.public void setRotation(double angleInDegrees)
angleInDegrees
- The new rotation amount, expressed as an
absolute angle in degrees.public void setBounds(android.graphics.RectF newBounds)
setBounds
in class Shape
newBounds
- The new bounding rectangle of the shape.public void setPositionAnchor(sofia.graphics.Anchor anchor)
setPositionAnchor
in class Shape
anchor
- The new position anchor.public void setPositionAnchor(android.graphics.PointF anchor)
setPositionAnchor
in class Shape
anchor
- The new position anchor.public void addOther(Shape newShape)
Shape
addOther
in class Shape
newShape
- The other shape to add.public void remove()
Shape
remove
in class Shape
public java.lang.String getNickName()
public void setNickName(java.lang.String nickName)
nickName
- The new nickname for this actor. If null, it
will "erase" any previously assigned name.public java.lang.String toString()
toString
in class Shape
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |