The ActorGroup2D Class
It is enough for several instances of a single Actor2D class to share images for animation. There are two basic ways to allow class instances to share the same data.
1) Include static class fields inside of the parent Actor2D class.
2) Pass an instance of a class that includes the single set of data to each Actor2D instance.
Static class fields may be all right for some instances, but for something like animation that is inherently complex and needs a lot of code, it would only cause confusion. Your Actor2D class would become quite large quickly, making it difficult to see through the clutter. Instead, pass an object to each Actor2D instance and give the appropriate methods for these two objects to communicate.
You may remember seeing this class within the implementation of the Actor2D class. This class extends upon the ImageGroup class so that it can give access to animation frames to Actor2D objects. This class adds additional fields for setting the maximum and minimum bounds and object velocity. Also use minimum and maximum tolerance values for both int and double types to serve as default values. All Actor2D objects must do is request these extreme values from their respective actor groups and compare them against their own position and velocity. ActorGroup2D subclasses are welcome to impose more minimum and maximum values.
Just take a quick glance at the ActorGroup2D class and note the extensions it makes over the ImageGroup class.
import java.applet.*;
// defines related attributes common to Actor2D objects
public abstract class ActorGroup2D extends ImageGroup
{
// default min/max values for int's and float's
protected static final int MAX_INT_UNBOUND = Integer.MAX_VALUE;
protected static final int MIN_INT_UNBOUND = Integer.MIN_VALUE;
protected static final double MAX_DBL_UNBOUND = Double.MAX_VALUE;
protected static final double MIN_DBL_UNBOUND = Double.MIN_VALUE;
// the maximum and minimum position and velocity an Actor2D can have
// overriding classes can change these values at construction time or within
// the init method
public int MAX_X_POS = MAX_INT_UNBOUND;
public int MAX_Y_POS = MAX_INT_UNBOUND;
public int MIN_X_POS = MIN_INT_UNBOUND;
public int MIN_Y_POS = MIN_INT_UNBOUND;
public int MAX_X_VEL = MAX_INT_UNBOUND;
public int MAX_Y_VEL = MAX_INT_UNBOUND;
public int MIN_X_VEL = MIN_INT_UNBOUND;
public int MIN_Y_VEL = MIN_INT_UNBOUND;
// constructs a new ActorGroup2D object
protected ActorGroup2D()
{
super();
}
// initializes shared Actor2D attributes
public abstract void init(Applet a);
} // ActorGroup2D
Indeed, the ActorGroup2D class is declared as abstract as well, so you will have to subclass it too. Fortunately, subclassing should be quick and painless, since the only required method to override is the init method.


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks