Java2 game programming study notes (10-2)
10.2.3 category expansion Actor2D
In order to create a realistic Actor2D vivid demonstration of procedures need to create three categories: Actor2D definition of the role of the sub-class, loading definition and image frame animation style ActorGroup2D object, all bringing together all the main events and drive cycle The Applet class. Therefore, after a short stay, next to a user to control the robot moving animation.
1.Robot category
Robot this category inherited from the Actor2D category, which defines an east, south, west and north direction of the four robots Amid this four direction, it can also use its weapons fire. Applet main categories can be defined and dealt with control mechanisms. In this category can only acceptable definition of robot behavior.
Below check the Robot category code, if it is confusing, it can turn around its reference to the father of finding out the circumstances surrounding the source code.
Import java.awt .*;
/ / Create a designated according to the direction of movement and the use of weapons can be fired at a simple robot
Public class Robot extends Actor2D (
/ / And the current animation associated Index
Protected int currAnimIndex;
/ / Animation design for the front of the preservation animation
Protected int prevAnimIndex;
/ / Robot opened fire on the state
Public final static int SHOOTING = 8;
/ / Used to tell the direction mobile robot
Public final static int DIR_NORTH = 0;
Public final static int DIR_SOUTH = 1;
Public final static int DIR_EAST = 2;
Public final static int DIR_WEST = 3;
/ / Used to be the role of the creation of a new group Robot
Public Robot (ActorGroup2D grp) (
Super (grp);
Vel.setX (5);
Vel.setY (5);
AnimWait = 3;
CurrAnimIndex = 0;
PrevAnimIndex = 0;
CurrAnimation = group.getAnimationStrip (RobotGroup.WALKING_SOUTH);
FrameWidth = currAnimation.getFrameWidth ();
FrameHeight = currAnimation.getFrameHeight ();
)
Public void update () (
If (hasState (SHOOTING)) (
Animate ();
)
Xform.setToTranslation (pos.getX (), pos.getY ());
UpdateBounds ();
CheckBounds ();
)
/ / Let robot firing method is called until stopShooting
Public void startShooting () (
PrevAnimIndex = currAnimIndex;
If (currAnimIndex% 2 == 0) (
CurrAnimIndex + +;
)
CurrAnimation = group.getAnimationStrip (currAnimIndex);
CurrAnimation.reset ();
SetState (SHOOTING);
)
/ / Stop shooting and return to the screen before shooting
Public void stopShooting () (
CurrAnimIndex = prevAnimIndex;
CurrAnimation = group.getAnimationStrip (currAnimIndex);
CurrAnimation.reset ();
ResetState (SHOOTING);
)
/ / Designated under the direction of, let robot to move.
Public void move (int dir) (
/ / Prevent excessive shooting
ResetState (SHOOTING);
Switch (dir) (
Case DIR_NORTH:
If (currAnimIndex! = RobotGroup.WALKING_NORTH) (
PrevAnimIndex = currAnimIndex;
Group.getAnimationStrip currAnimation = (
RobotGroup.WALKING_NORTH);
CurrAnimIndex = RobotGroup.WALKING_NORTH;
CurrAnimation.reset ();
Else ()
Animate ();
Pos.translate (0,-vel.getY ());
)
Break;
Case DIR_SOUTH:
If (currAnimIndex! = RobotGroup.WALKING_SOUTH) (
PrevAnimIndex = currAnimIndex;
Group.getAnimationStrip currAnimation = (
RobotGroup.WALKING_SOUTH);
CurrAnimIndex = RobotGroup.WALKING_SOUTH;
CurrAnimation.reset ();
Else ()
Animate ();
Pos.translate (0, vel.getY ());
)
Break;
Case DIR_WEST:
If (currAnimIndex! = RobotGroup.WALKING_WEST) (
PrevAnimIndex = currAnimIndex;
Group.getAnimationStrip currAnimation = (
RobotGroup.WALKING_WEST);
CurrAnimIndex = RobotGroup.WALKING_WEST;
CurrAnimation.reset ();
Else ()
Animate ();
Pos.translate (-vel.getX (), 0);
)
Break;
Case DIR_EAST:
If (currAnimIndex! = RobotGroup.WALKING_EAST) (
PrevAnimIndex = currAnimIndex;
Group.getAnimationStrip currAnimation = (
RobotGroup.WALKING_EAST);
CurrAnimIndex = RobotGroup.WALKING_EAST;
CurrAnimation.reset ();
Else ()
Animate ();
Pos.translate (vel.getX (), 0);
)
Break;
Default:
Break;
)
)
) / / Robot
Robot category, it should be noted that static final SHOOTING attribute the use to populate it here 8 (2 3-th power), so that it will not interfere with or Actor2D coverage of the state variables. If you need a refresher in how to conduct operations, you can see Chapter 2
Can now continue with the second step: creating a RobotGroup category. This class of the eight animation series loaded, one walk in all directions, is a fire in all directions. Robot category then it can be under the direction of the face of the current choice of animation. If readers have greater interest in this chapter can look at the practice, a practice that in a cycle of demand in all the four loading animations come here only for the purpose of presentation in the cycle have not done so .
Import java.applet .*;
Public class RobotGroup extends ActorGroup2D (
/ / Pre-defined animation sequence
Public static final int WALKING_NORTH = 0;
Public static final int SHOOTING_NORTH = 1;
Public static final int WALKING_SOUTH = 2;
Public static final int SHOOTING_SOUTH = 3;
Public static final int WALKING_EAST = 4;
Public static final int SHOOTING_EAST = 5;
Public static final int WALKING_WEST = 6;
Public static final int SHOOTING_WEST = 7;
/ / Create a new object RobotGroup
Public RobotGroup () (
Super ();
Animations = new AnimationStrip [8];
)
/ / Initialization eight animation sequence
Public void init (Applet a) (
ImageLoader loader;
Int i;
/ / North
Loader = new ImageLoader (a, "robot_north.gif", true);
Animations [WALKING_NORTH] = new AnimationStrip ();
For (i = 0; i <4; i + +) (
Animations [WALKING_NORTH]. AddFrame (
Loader.extractCell ((i * 72) + (i +1), 1,72,80));
)
Animations [WALKING_NORTH]. SetAnimator (new Animator.Looped ());
Animations [SHOOTING_NORTH] = new AnimationStrip ();
For (i = 0; i <2; i + +) (
Animations [SHOOTING_NORTH]. AddFrame (
Loader.extractCell ((i * 72) + (i +1), 82,72,80));
)
Animations [SHOOTING_NORTH]. SetAnimator (new Animator.Looped ());
/ / South
Loader = new ImageLoader (a, "robot_south.gif", true);
Animations [WALKING_SOUTH] = new AnimationStrip ();
For (i = 0; i <4; i + +) (
Animations [WALKING_SOUTH]. AddFrame (
Loader.extractCell ((i * 72) + (i +1), 1,72,80));
)
Animations [WALKING_SOUTH]. SetAnimator (new Animator.Looped ());
Animations [SHOOTING_SOUTH] = new AnimationStrip ();
For (i = 0; i <2; i + +) (
Animations [SHOOTING_SOUTH]. AddFrame (
Loader.extractCell ((i * 72) + (i +1), 82,72,80));
)
Animations [SHOOTING_SOUTH]. SetAnimator (new Animator.Looped ());
/ / East
Loader = new ImageLoader (a, "robot_east.gif", true);
Animations [WALKING_EAST] = new AnimationStrip ();
For (i = 0; i <4; i + +) (
Animations [WALKING_EAST]. AddFrame (
Loader.extractCell ((i * 72) + (i +1), 1,72,80));
)
Animations [WALKING_EAST]. SetAnimator (new Animator.Looped ());
Animations [SHOOTING_EAST] = new AnimationStrip ();
For (i = 0; i <2; i + +) (
Animations [SHOOTING_EAST]. AddFrame (
Loader.extractCell ((i * 72) + (i +1), 82,72,80));
)
Animations [SHOOTING_EAST]. SetAnimator (new Animator.Looped ());
/ / West
Loader = new ImageLoader (a, "robot_west.gif", true);
Animations [WALKING_WEST] = new AnimationStrip ();
For (i = 0; i <4; i + +) (
Animations [WALKING_WEST]. AddFrame (
Loader.extractCell ((i * 72) + (i +1), 1,72,80));
)
Animations [WALKING_WEST]. SetAnimator (new Animator.Looped ());
Animations [SHOOTING_WEST] = new AnimationStrip ();
For (i = 0; i <2; i + +) (
Animations [SHOOTING_WEST]. AddFrame (