J2ME Game Development: Issues and Troubleshooting(TM) - A more practical example
by Massimo Perrone
4.1 A more practical example
In the end i wanted to show a practical example about problems arising in the
game development: handling of animations for the players on screen.
To achieve this, we already said that we need to know the exact status of the
players, time by time, during game.
Let's assume, furtherly, as follow:
frames of the players are loaded inside an array;
we have seven frames for each player and each one is related to a status
of the players
pictures will strictly follow the same sequence: move to left, move to right,
punch, kick, block, player hit, player defeated
first animation set will be that of the player, the following ones will contain
the opponents.
Once we assume all of this we can write a routine that handles, in a simple way,
the animations:
public void drawFighters()
{
...
if (imagePlayer==0)
{
statePlayer= commandPlayer;
if(commandPlayer==PUNCH||commandPlayer==KICK||commandPlayer==HIT)
commandPlayer=NONE;
}
int imageNumPlayer=0;
if (statePlayer==BLOCK) imageNumPlayer=4;
else if (statePlayer==MOVELEFT)
{
xPlayer-=4;
if (xPlayer<-4) xPlayer=-4;
imageNumPlayer=imagePlayer;
if(imagePlayer>=1) imagePlayer=0;
else imagePlayer++;
}
else if (statePlayer==MOVERIGHT)
{
xPlayer+=4;
if (xEnemy-xPlayer<20) xPlayer=xEnemy-20;
imageNumPlayer=imagePlayer;
if(imagePlayer>=1) imagePlayer=0;
else imagePlayer++;
}
else if (statePlayer==PUNCH)
{
if (imagePlayer<2)
{
xPlayer+=4;
if (xEnemy-xPlayer<20) xPlayer=xEnemy-20;
imageNumPlayer=2;
}
if(imagePlayer>=2) imagePlayer=0;
else imagePlayer++;
}
else if (statePlayer==KICK)
{
if (imagePlayer<2)
{
xPlayer+=4;
if (xEnemy-xPlayer<20) xPlayer=xEnemy-20;
imageNumPlayer=3;
}
if(imagePlayer>=2) imagePlayer=0;
else imagePlayer++;
}
else if (statePlayer==HIT)
{
xPlayer-=6;
if (xPlayer<1) xPlayer=1;
imageNumPlayer=5;
if(imagePlayer>=2) imagePlayer=0;
else imagePlayer++;
}
else if (statePlayer==ONFLOOR)
{
xPlayer-=6;
if (xPlayer<2) xPlayer=2;
imageNumPlayer=6;
if(imagePlayer>=1) imagePlayer=1;
else imagePlayer++;
}
else imagePlayer=0;
...
}
All the code deals with control of the player's animation; the same should be
done, in a similar way, to handle the animation of the CPU controlled opponent.
Routine is about reacting to user or CPU commands and showing, for a couple
animations, the frame corresponding to the current status of the fighter.
By mean of the "imagePlayer" variable we control whether we have to show the
base frame (the 0) or the one related to the status detected. Moreover, in
case of moves, the variable "xPlayer" ("xEnemy" in case of the opponent) is
consequently updated.
In the end, according to the status detected, the variable "imageNumPlayer"
will be updated with the frame number that will be shown for the opponent
involved. Of course also the code supervising control of the CPU must be
based on a variable with a similar name like e.g. "imageNumEnemy".
With these two variables we can later plot our fighters on screen for each
new frame in this way:
g.drawImage(picFighters[imageNumEnemy+(7*level)],xEnemy,yPos,offGraphics.TOP|offGraphics.LEFT);
g.drawImage(picFighters[imageNumPlayer],xPlayer,yPos,offGraphics.TOP|offGraphics.LEFT);
As you can see, these two variables "imageNumPlayer" and "imageNumEnemy" are used as
index to load the correct picture. The variable "level" shows the game level we are
playing so that in case several enemies are to be challenged we can easily find
the right frame to plot by shifting forward by 7 steps according to the level number
we're playing.
"xEnemy" and "xPlayer" variables are calculated too each time from the routine itself
and determine the horizontal position of the fighters on screen, last one is the
"yPos" variable that has a constant value determined at time of screen height detection
of the given device.
Next Page >>
|