|
|||||||||
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
sofia.micro.ScriptableActor
public class ScriptableActor
Represents an Actor that is controlled by a script--that is, a
predefined sequence of behavior played out over time. There are
two ways to provide a script for an actor: either override the
script()
method in a subclass (most common for beginner
programmers), or create a script as a separate object and pass
it into setScript(Script)
(more advanced, and more useful
if there may be multiple scripts that might be associated with a
given actor).
Constructor Summary | |
---|---|
ScriptableActor()
Create a new scriptable actor. |
|
ScriptableActor(boolean scaleToCell)
Create a new scriptable ctor. |
|
ScriptableActor(java.lang.String nickName)
Create a new scriptable actor. |
|
ScriptableActor(java.lang.String nickName,
boolean scaleToCell)
Create a new scriptable actor. |
Method Summary | ||
---|---|---|
protected void |
_gf_addToWorld(int x,
int y,
greenfoot.sofiainternal.World world)
|
|
void |
act()
This implementation of the act method executes one action in this actor's script. |
|
Script |
getScript()
Get the script associated with this actor. |
|
void |
move(int distance)
Move this actor the specified distance in the direction it is currently facing. |
|
void |
script()
Subclasses can override this method to provide the "script" for the actor to follow. |
|
protected void |
scriptStep()
Triggers one action in this actor's script. |
|
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 |
setRotation(double rotation)
Set the rotation of this actor. |
|
|
setScript(ActorScript<MyActor> script)
Associate a script with this actor by providing an ActorScript
object. |
|
void |
setScript(Script script)
Associate a script with this actor by providing a Script
object. |
|
void |
stopScript()
Stop any currently executing script associated with this 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.micro.Actor |
---|
addedToWorld, addOther, getGridX, getGridY, getImage, getIntersectingObjects, getNeighbors, getNickName, getObjectsAtOffset, getObjectsInRange, getOneIntersectingObject, getOneObjectAtOffset, getShape, getWorld, remove, setBounds, setImage, setImage, setNickName, setPosition, setPosition, setPositionAnchor, setPositionAnchor, setX, setY, toString |
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_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 ScriptableActor()
public ScriptableActor(java.lang.String nickName)
nickName
- The nickname for this actor.public ScriptableActor(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 ScriptableActor(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()
Actor
as your base
class.
If, on the other hand, you want to combine the features of
a scriptable actor with a custom act() method, you can do that by
inheriting from this class and overriding this method. If you
override this method, be sure to call super.act()
or
your actor will no longer obey its assigned script.
act
in class Actor
public void move(int distance)
The direction can be set using the Actor.setRotation(double)
method.
move
in class Actor
distance
- The distance to move (in cell-size units). A
negative value will move backwards.Actor.setGridLocation(int, int)
public void turn(double amount)
turn
in class Actor
amount
- The number of degrees to turn. Positive values turn
clockwise.Actor.setRotation(double)
public void turnTowards(int x, int y)
turnTowards
in class 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)
turnTowards
in class Actor
target
- The actor to turn towards.public void setRotation(double rotation)
setRotation
in class Actor
rotation
- The new rotation amount, expressed as an
absolute angle in degrees.public void setGridX(int x)
setGridX
in class Actor
x
- The location to move to on the x-axis.Actor.setGridLocation(int, int)
public void setGridY(int y)
setGridY
in class Actor
y
- The location to move to on the y-axis.Actor.setGridLocation(int, int)
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.
setGridLocation
in class Actor
x
- The location to move to on the x-axis.y
- The location to move to on the y-axis.Actor.move(int)
public void script()
script
in interface Script
public <MyActor extends ScriptableActor> void setScript(ActorScript<MyActor> script)
ActorScript
object. Actions in the script will execute one move at a time as
act() is called. A script value of null will remove any assigned
script for this actor.
MyActor
- The type of actor this script is written
for, which should be the same as this actor's
type (or one of its supertypes).script
- The script to activate.public void setScript(Script script)
Script
object. Actions in the script will execute one move at a time as
act() is called. A script value of null will remove any assigned
script for this actor.
script
- The script to activate.public Script getScript()
public void stopScript()
protected void scriptStep()
protected void _gf_addToWorld(int x, int y, greenfoot.sofiainternal.World world)
_gf_addToWorld
in class greenfoot.sofiainternal.Actor
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |