Actor

greenfoot Class Actor
code java.lang.Object

code

code public abstract class Actor code extends java.lang.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). //Actor这个类别存在于Greenfoot world这个大的对象中，world类记录了Actor的位置，并把它显示出来（作为一个图标的形式）

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.

Actor并不是一个普通的实例，而是作为一个超类存在的，每个需要在world类中显示的对象都要继承Actor. 子类可以定义它们自己的显示和动作方法，这个类的最重要的一个方法是act,这个方法在按钮Act或者Run按下时被调用. 原始方法是空的，需要用户添加.

Constructor Summary Actor Construct an Actor.
 * Actor的构造方法：**

Method Summary void act The act method is called by the greenfoot framework to give objects a chance to perform some action. protected void addedToWorld(World world) This method is called by the Greenfoot system when the object has been inserted into the world. //本方法在对象加入到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 the object. //得到对象的旋转速度 World getWorld Return the world that this object lives in. //得到Actor对象存在的world对象 int getX Return the x-coordinate of the object's current location. //得到Actor对象的X坐标 int getY Return the y-coordinate of the object's current location. //得到Actor对象的Y坐标 protected boolean intersects(Actor other) Check whether this object intersects with another given object. //检测此对象是否与其他对象有交叉 void setImage(GreenfootImage image) Set the image for this object to the specified image. //给本对象设置显示图片 void setImage(java.lang.String filename) Set an image for this object from an image file. //从本地中提取图片设置为对象的显示图片 void setLocation(int x, int y) Assign a new location for this object. //设置对象的显示位置 void setRotation(int rotation) //设置对象的旋转速度 Set the rotation of the object. Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait //这些方法继承自java.lang.Object
 * Actor的其他方法（继承来的）：**

Constructor Detail Actor public ActorConstruct an Actor. The object will have a default image.

Method Detail act public void actThe act method is called by the greenfoot framework to give objects 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 object's action.

getX public int getX throws java.lang.IllegalStateExceptionReturn the x-coordinate of the object's current location. The value returned is the horizontal index of the object's cell in the world.

Returns: The x-coordinate of the object's current location. Throws: java.lang.IllegalStateException - If the actor has not been added into a world.

getY public int getYReturn the y-coordinate of the object's current location. The value returned is the vertical index of the object's cell in the world.

Returns: The y-coordinate of the object's current location Throws: java.lang.IllegalStateException - If the actor has not been added into a world.

getRotation public int getRotationReturn the current rotation of the object. Rotation is expressed as a degree value, range (0..359). Zero degrees is to the east. The angle increases clockwise.

Returns: The rotation in degrees. See Also: setRotation(int)

setRotation public void setRotation(int rotation)Set the rotation of the object. Rotation is expressed as a degree value, range (0..359). Zero degrees is to the east, and the angle increases clockwise.

Parameters: rotation - The rotation in degrees.

setLocation public void setLocation(int x, int y)Assign a new location for this object. The location is specified as a cell index 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 Throws: java.lang.IllegalStateException - If the actor has not been added into a world.

getWorld public World getWorldReturn the world that this object lives in.

Returns: The world.

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

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

getImage public GreenfootImage getImageReturns the image used to represent this Actor. This image can be modified to change the object's appearance.

Returns: The object's image.

setImage public void setImage(java.lang.String filename) throws java.lang.IllegalArgumentExceptionSet an image for this object 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: java.lang.IllegalArgumentException - If the image can not be loaded.

setImage public void setImage(GreenfootImage image)Set the image for this object to the specified image.

Parameters: image - The image. See Also: setImage(String)

intersects protected boolean intersects(Actor other)Check whether this object intersects with another given object.

Returns: True if the object's intersect, false otherwise.

getNeighbours protected java.util.List getNeighbours(int distance, boolean diagonal, java.lang.Class 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.

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 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).

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, java.lang.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 java.util.List getObjectsInRange(int radius, java.lang.Class 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'.

Parameters: radius - Radius of the circle (in cells) cls - Class of objects to look for (passing 'null' will find all objects).

getIntersectingObjects protected java.util.List getIntersectingObjects(java.lang.Class cls)Return all the objects that intersect 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).

getOneIntersectingObject protected Actor getOneIntersectingObject(java.lang.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).