J2ME Game Development: Issues and Troubleshooting (TM) -Deeper in the Game Logic
by Massimo Perrone (Hanami Solutions)
4.0 Deeper in the game logic
Let's dive further in the game logic.
Inside the game() method, will take place the most disparate tasks, amongst
them in the case of our beat-em-up:
public void game()
{
plotBackground();
checkKeysPressed();
checkCPU();
updatePlayerStatus();
updateEnemyStatus();
checkGameStatus();
drawFighters();
}
As you can see everything proceeds whithin the 20 milliseconds in a logical
sequence.
First of all we will draw the background and there is no need for explanations about
this.
Later on we check if any key has been pressed meanwhile on the device.
This is a delicate operation so i'll briefly sum up the better way to do it.
Canvas class provide us with two extremely useful methods: keyPressed() and
keyReleased(). Since the whole game system must be based on a quick feedback
from these two methods, best way is to not handle animation process from inside
them but solely get input and forward its result to other methods like, (in this
case: checkCPU(), updatePlayerStatus() and updateEnemyStatus())
so to make the key press function available again the sooner is possible.
Method keyPressed() shall then contain something like this:
public void keyPressed(int keyCode)
{
...
int action = getGameAction(keyCode);
if (action==FIRE||keyCode==KEY_NUM5) commandPlayer=BLOCK;
else if (action==LEFT||keyCode==KEY_NUM4) commandPlayer=MOVELEFT;
else if (action==RIGHT||keyCode==KEY_NUM6) commandPlayer=MOVERIGHT;
else if (action==UP||keyCode==KEY_NUM2) commandPlayer=PUNCH;
else if (action==DOWN||keyCode==KEY_NUM8) commandPlayer=KICK;
...
}
This way we've quickly received user input and the method has been freed istantly
for other key presses. The methods following it will be in charge of handling
consequences of user's choice on the game proceedings.
Whithin the code you'll notice variables like "BLOCK", "MOVELEFT", etc.
I've used variables of the byte kind with names matching player status.
Of course each variable must have different value, e.g. i've used:
NULL=-1,MOVELEFT=0,MOVERIGHT=1,PUNCH=2,KICK=3,BLOCK=4,HIT=5,ONFLOOR=6;
These values will be later be stored, time by time, in "commandPlayer" to
know, each moment, what's happening to the player.
In the same way, by mean of method checkCPU() we will have to evaluate
according to user's choices, how the artificial intelligence of the current
enemy should react and saving in a specific variable ("commandEnemy")
what's happening to the current enemy.
By doing so, we will have recorded both the current status of the player
and that of his enemy that we will update later with its related methods
updatePlayerStatus() and updateCPUStatus().
Inside these two methods, we will take care of, again, by using aforementioned
variables, checking if the player and/or his enemy has been hit, if they
move rightwards or leftwards, whether they blocked an attack, etc.
At this point, because of what happened up to now, we need to check again
if the game must continue, if we reached the end of a round and/or of a
match winning it or if it is "game over".
According to the game status that we will handle with appropriate variables,
similar to those used for status of player and his enemy, we will know where
and how to show the opponents and then finally launch the drawFighters()
method to see the result on screen.
Next Page >>
|