J2ME development of mobile phone games: Gem box (job is reposted)
Abstract: J2ME development of mobile phone games: Gem box (job is reposted)
From: 61 IC China's electronics online
I have recently developed a J2ME with the color MotoT720 game: stones box (GridOne). In the process of developing accumulated some experience, it is now write to share with you.
Use double buffering to avoid screen flicker
Double buffering is the preparation of technical procedures J2ME game one of the key technologies. In fact, double buffering technology is a traditional computer animation technology. The main reason causing the screen flashes images in the show at the same time, also in the process to change it, the screen was flickering. Solution is to open up in memory region as a background screen, and its procedures to update, modify, display it after completion. This image is displayed is always has been fully drawn images, will not be revised procedures are being displayed image. Of course, there are other ways to solve the problem of screen flicker, but the use of double buffering technology is a recommendable solution. Specific methods can be found in the following code:
Public class BlocksCanvas extends Canvas implements Runnable
(
Graphics bg;
Image buf;
Public BlocksCanvas ()
(
……
Height = getHeight ();
Width = getWidth ();
/ / Size of the screen on the establishment of buffer object buf = Image.createImage (width, height);
/ / Buffer attached to the Graphics object bg
Bg = buf.getGraphics ();
……
)
Public void run ()
{……
For (i = 0; i (
For (j = 0; j (/ / painting box drawBlock (x, y);
)
)
Repaint ();
)
Private void drawBlock (int block_x, int block_y)
(
/ / Obtain the coordinates of the box int x = getLeft (block_x);
Int y = getTop (block_y);
/ / The color of the box board int c = [] [block_x block_y];
Bg.drawImage (imgs [c], x, y, Graphics.TOP | Graphics.LEFT);
)
Public void paint (Graphics g)
(
G.drawImage (buf, 0, 0, Graphics.TOP | Graphics.LEFT);
)
)
Visible from the code above, double buffering thinking of the procedure followed is to complete the following steps:
1. A definition and a Graphics object bg Image object buf, by the establishment of a screen size of the buf buffer object, and then made buf of the Graphics object to the bg. Here, Graphics objects can be understood as a buffer screen, Image object can be as a buffer to the picture on the screen.
2. In bg (buffer screen) with drawImage () and drawString statements such as drawing, or painting in the buffer on the screen.
3. Call repaint () statements, and its function is to tell the system calls paint () to complete the true screen display. It should be noted that the paint () system call is a statement, not manual call, only through the paint () statements to call.
4. In the paint (Graphics g) function, will buf (the picture on the screen buffer) to the real painting on the screen.
Although the steps seem more complicated, but the effect is very good. If you want to show on the screen what to do in the bg painting, and then call repaint () will be displayed on its grounds.
Prepare their own breakpoints function
J2ME procedures in the development process, the most troubled people is the issue of procedures baffling Dead easy. When using JBuilder CodeWarrior set breakpoints or function to find procedural errors, crashed on the probability of greater. If not crippling, procedures will be worried by the accident interference, it is generally not recommended to use development tools onboard breakpoint functionality. But sometimes also need a function to display the current value of each variable to make the right Chaixi when the judgement. So I think a way, it is preparing its own breakpoints function. Specific code as follows:
Public class BlocksCanvas extends Canvas implements Runnable
(
Private boolean stopFlag = false; / / debugging signs ……
Public void run ()
(
/ / Breakpoint position 1
TestFun ( "x:" + x + "y:" + y);
……
/ / Breakpoint location 2
TestFun ("");
……
)
Private void testFun (String str)
(
StopFlag = true;
/ / Draw a white rectangle bg.setGrayScale (255);
Bg.fillRect (0,0, fontW, fontH);
/ / Str white rectangle on the display the contents of bg.setGrayScale (0);
Bg.drawString (str, 0,0, Graphics.TOP | Graphics.LEFT);
Repaint ();
While (stopFlag) ()
)
Public void keyPressed (int keyCode)
(
StopFlag = false;
)
)
First definition of a boolean type of variables to record debugging stopFlag signs. It's a start value of false, entered testFun () function, as a true value. End str show the contents, because the value of true stopFlag, while statements entered a cycle of death, closed down procedures. Then be careful look at the value of variables. Then when the press any button, keyPressed () function to capture this event, will be set to false stopFlag death cycle automatically solved. Use of this method is very convenient, as long as the required breakpoint locations are testFun () statement can be, a number of procedures can be placed testFun () statement in the loop can also be placed testFun () statements. Running to testFun () statement will automatically stop display variable value, by any button procedures will be run automatically, and procedures would not be unexpected interference. Figure 1 is the title when debugging.
Another point to describe this method of testFun () statement must be on the run () function or run () function call a function of operation, while it will be because () occupied all the CPU time lead keyPressed () Function keys can not capture the incident, which leads to freeze.
As long as this method slightly modified, can be used to suspend the game, and also than the sleep () method, and, after all, theoretically sleep () method can not continue indefinitely suspended. Here is the corresponding code:
Public class BlocksCanvas extends Canvas implements Runnable
(
Private boolean stopFlag = false; / / suspended signs ……
Public void run ()
{……
TestFun ();
……
)
Private void testFun ()
(
While (stopFlag) ()
)
Public void keyPressed (int keyCode)
(
Int action = getGameAction (keyCode);
If (action == FIRE) stopFlag =! StopFlag;
)
)
The procedure of the functions when users press the FIRE button, the game was suspended again press the FIRE button, the game continued to operate.
Prepare their own tools category
Because of memory and function of cell phone restrictions, J2ME provided only part of the J2SE tool for users of call. So sometimes we have to prepare their own tools to achieve some type of special functions. Below is similar to the type of kSet Set J2SE tool in the functional category. It is used to record the game was deleted in the box set, but did not guarantee the same set of elements.
/ **
*
Description: Set in the category on the realization of J2ME
*
Date: 2003.2.28
*
Author: TomJava
*
Email: tomjava@sohu.com
* /
Public class kSet
(/ / Single-linked list to achieve private kSetNode head;
Public kSet ()
(
Head = null;
)
/ / Public void clear kSet Empty ()
(
Head = null;
)
/ / Add elements to kSet public boolean add (int x, int y)
(
KSetNode node = new kSetNode (x, y);
Return add (node);
)
/ / Add elements to kSet public boolean add (kSetNode node)
(
If (! Contains (node))
(
Node.next = head;
Head = node;
Return true;
) Else
(
Return false;
)
)
/ / KSet judge whether empty public boolean isEmpty ()
(
If (head == null)
Return true;
Else
Return false;
)
/ / Listless won the first element of this element and return to the public kSetNode getFirst ()
(
KSetNode p = head;
= P.next head;
Return p;
)
/ / Traversing kSet, if the same elements returns true, otherwise returns false
Public boolean contains (kSetNode node)
(
KSetNode p = head;
While (p! = Null) (
If (p.equals (node)) return true;
P = p.next;
)
Return false;
)
)
/ / KSet elements in the public class kSetNode
(
Public int x, y;
Public kSetNode next;
Public kSetNode (int x, int y)
(
This.x = x;
This.y = y;
Next = null;
)
Public boolean equals (kSetNode node)
(
If (x == & node.x node.y == y)
Return true;
Else
Return false;
)
Public int getX ()
(
Return x;
)
Public int getY ()
(
Return y;
)
)
KSetNode category responsible for recording the coordinates of the box was deleted, it override equals () method used to determine whether two box with a box. KSet category is composed of objects from kSetNode not the same element of the set, achieving a single linked list, and provided getFirst (), add (), clear (), isEmpty (), contains () method calls for the other classes. Preparation and use of some of these tools will greatly speed up the pace of programming, and the procedure becomes more clear.
Correction screen coordinates
GridOne this game is designed for MotoT720 development, which means that the game MotoT720 background picture size and the size of cell phones is the same. If it MotoT720 than in those big-screen phones running game background picture will be displayed in the upper left corner of the screen and aesthetic effects, when used to screen Correction technology, the middle game background images showed. Correction screen coordinates of the code below:
Public class BlocksCanvas extends Canvas implements Runnable
(
Private final int addX; / / coordinates correction private final int addY;
Private final int SCREEN_X; / / screen vertex private final int SCREEN_Y;
Private final int WAITBLOCK_X; / / wait for the box vertex private final int WAITBLOCK_Y;
Private final int SCORES_X; / / vertex scores of private final int SCORES_Y;
Private final int STAR_X; / / pentacle the apex of private final int STAR_Y;
Public BlocksCanvas ()
(
/ / Get the current mobile phone screen height and width height = getHeight ();
Width = getWidth ();
/ / Correct coordinates of addX = (width-120) / 2;
AddY = (height-142) / 2;
/ / Initialization parameters SCREEN_X screen addX + = 48; / / vertex SCREEN_Y screen addY + = 10;
WAITBLOCK_X addX + = 19; / / wait for the box vertex WAITBLOCK_Y addY + = 103;
SCORES_X addX + = 36; / / vertex SCORES_Y Score addY + = 34;
STAR_X addX +4 = / / = STAR_Y the culmination of five-pointed star addY +70;
)
)
First of all, the screen all relevant parameters are defined as private final int type variables. Here is the final Xiuchifu is because they do not want the initial value of variables, their value will change; Xiuchifu is not static, because its function to initialize variables, and not in the definition when initialized it. To use getHeight () and getWidth () function in the cell phone screen height and width, the need to calculate and offset addX addY, and then added to the screen parameters, such games will be shown the middle. Figure 2 and Figure 3 shows results comparison chart.
Another point needs attention, with getHeight () to the screen is not real high, but the height of the screen height minus Command label, because the screen need to stay in a local show Command label.
Rational use of memory
Originally the use of the Java programming concerned about memory usage is not necessary, because Java has its pride waste disposal mechanism. However, to J2ME, the situation has changed because of the memory cell phone only a handful of the several hundred K, can no longer as in the J2SE, as extravagantly used. Otherwise, we will find that even if there is no procedure syntax and logic errors, we can not run in the simulator. Here is the rational use of memory few suggestions:
1. Used as much as possible instead of the members of the local variables, reducing object creation, the best re-use of objects;
2. Do not attempt to initialize all the time or Form Canvas object read into memory, but should be in the time needed to create, although this shows that there will be some delays, but the process can not run better than memory overflow or better;
3. Once the object does not need to use the time for their home null to refuse processor can be recovered, the appropriate time calling System.gc () statement suggested that virtual machine called garbage processor;
4. Must remember Java memory management is a mechanism to the edge, which is why the use of the object, and under no circumstance is the use of the object at it, not to avoid memory recovery;
5. Picture, as far as possible possession of a small number of bytes, you can use Fireworks in ensuring picture quality picture at the same time reduce the size;
Some even do the above, we can not guarantee that will not happen memory leak procedures, because, after all, so little memory cell phone. Therefore, I submit the final proposal is to complete the main part of the game to enable it to operate normally and no memory leaks, then slowly expanded games, it added to the front and other functions. Once a memory and then remove some features.
Make games more attractive
Preparation of the game certainly hope that it can attract, I think the following points deserve attention:
1. Attention control the rhythm of the game
Originally, I trimmed away in the box, nothing to do directly delete, and then to start a new cycle, waiting to descend the box. In the actual running time is not feeling very good results, because the sooner cut the top off the box also faster, players have a feeling unprepared. Later, I delete the box, with an empty cycle pause a few seconds, the players gave such a reaction time, I felt that the better, but if cut, the reaction time will be longer, in this game makes play, there is a relaxed feeling. Then, I will air circulation into the box on the screen traversing three times, was cut to the flickering box, making gamers can look at the deleted box to enjoy the fruits of their own, this has also increased the game appeal.
2. Colorful pictures of achieving results
In the case of the J2ME control the size of text and fonts, then this will make in reducing the effects of the game. However, through the various special text made picture approach to solve this problem. In some places, we can also use images to replace text, makes games more vivid, for example, in the column with five-pointed star rating to express the difficulty of the game. One of the advantages of using the picture there is the general increase in the game, making the game different in the mobile phone display is basically the same. In addition, if a font color in the simulator showed normal, and not displayed on the phone in a normal situation, can be resolved in this manner.
3. The game will automatically adjust difficulty
I use the following function, making games becoming increasingly difficult.
Private void giveLevel ()
(
If (level <= 10)
(
LevelSleep = 600 - (level - 1) * 50;
If (levelSleep <200)
LevelSleep = 200;
LevelDelTask = 200 + (level-1) * 100;
If (levelDelTask> 1000)
LevelDelTask = 1000;
/ / Painting class - five-pointed star paintStar ();
) Else
(
GameWin ();
)
)
Grade level that game, levelSleep said that the whereabouts of a box of waiting time, levelDelTask expressed a need to remove the Commissioner of the box can also be understood as a hurdle to complete the task. The above formula can guarantee that the game will become more and more difficult to increase the attractiveness of the game.
↑ Back
Tags: mobile java






