Gamecanvas is one of the most important elements of MIDP2.0, improve the convenience of J2ME game development, compared to MIDP1.0, reduce the difficulty and cost of J2ME game development, this article specifically detailed the Gamecanvas game canvas of the relevant content, the Gamecanvas has a general analysis.
The main methods, methods and methods in Gamecanvas are summarized as follows:
1, the method Gamecanvas, the method prototype protected Gamecanvas (Boolean suppresskeyevents), the function: Constructs the method, the parameter suppresskeyevents Indicates whether you need to handle key events other than the game keys. For example, if this parameter is false, the key event handling method keypressed, keyrepeated, keyreleased is not invoked while the program is running, which can improve speed and performance.
2, Method Getgraphics, method prototype protected Graphics Getgraphics (), method effect: Get the Graphics object for drawing on the offline screen of canvas.
3, Method Flushgraphics, method prototype public void Flushgraphics (), method function: Request to refresh the screen, then the image on the offline screen will be drawn to the real screen.
4. Method Flushgraphics, method prototype public void Flushgraphics (int x, int y, int width, int height), method action: Requires refreshing the specified area on the screen, The image of the specified area on the offline screen is then drawn to the real screen.
5, Method Getkeystates, method prototype public int getkeystates (), method effect: Get key state.
Second, Gamecanvas is to facilitate the game development of the canvas class, from the class implementation, you can see that the Gamecanvas class is canvas class subclass, It inherits the Javax.microedition.lcdui.Canvas class in use in MIDP1.0, so the canvas 備置 method can use Gamecanvas directly.
Third, Gamecanvas the biggest feature is to support the acquisition of offscreen buffer and key state.
1, the so-called offscreen buffer, refers to the scene outside the picture, and then send the results to the actual picture of the system, so, with this function, you can prevent the display of animation when the picture of the phenomenon.
2, in the previous MIDP1.0 application, when the key is pressed, the JVM needs to call out the Keypressed method, so it is easy to understand the status of the key. This method is called event-driven, but this method is difficult to directly embody the action in the game when the key is pressed down to the execution of the method, so the Gamecanvas can check the button status and the updated description. Canvas class, the key to be pressed when the method keypressed and the method of paint through the JVM's judgment to execute, so the close connection with the game state is more difficult. The Gamecanvas class, developers can view the state of the button at any time, performing a further description, so it is convenient for the game to manage the situation.
Iv. code Examples
1, Key state
// 获取按键状态
int keyStates = getKeyStates();
// 根据按键状态做相应处理
if ((keyStates & LEFT_PRESSED) != 0) {
// 左键按下时候处理代码
} else if ((keyStates & RIGHT_PRESSED) != 0) {
// 右键按下时候处理代码
} else if ((keyStates & UP_PRESSED) != 0) {
// 向上键按下时候处理代码
} else if ((keyStates & DOWN_PRESSED) != 0) {
// 向下键按下时候处理代码
}
2, call redraw
Update the description content flushgraphics ();