KJAVA MIDP 2.0: The Game API Quick Start

zhaozj2021-02-17  47

MIDP 2.0: The Game API

By Mikko Kontio-01/09/2003

Translation: bolow

MIDP 2.0 provides mobile devices with a number of new features, mainly in media support, enhanced UI interfaces, more network protocols, OTA, and security. However, most of us is most interesting is the new feature of the game API. This article introduces new game API classes and their usage by some examples. Example debugging environment is J2ME Wireless Toolkit 2.0 Beta.

Game API

Game API helps developers (game developers or other developers who need better UI interfaces) help users quickly develop practical and save memory and storage space. Using MIDP 1.0 GAME developers must define their own image classes to get good interfaces and program performance, which will inevitably increase the overhead of program storage, making your JAR file bigger. The new game API can solve these problems.

The basic idea of ​​the game API is that the game interface consists of layers. The background can be on a layer, while the game character can be on another layer. Each layer can be controlled by the game API. According to experience, the game's racemat will usually be larger than the screen of the phone, so the scroll to control the screen in the traditional method is a very painful thing. A new game API a new view window, which can see all game scenes, and it can be very easy to move.

The path to the game API is javax.microedition.lcdui.game. These five new classes are: Gamecanvas, Layer, LayerManager, Sprite, Tiledlayer.

Gamecanvas is an abstract class that provides the basic interface of the game. This class has two advantages over the Canvas class: 1. It has a screen buffer, 2. It can get directly to the physical state of the device keyboard.

Layer is an abstract class that defines the game elements. Sprite and TiledLayer inherited this class. Layer is a very common class.

LayerManager is responsible for managing the Layer object and painting them in the specified order.

Sprite contains a number of frame images of the image. These frames are saved in the Image object. With the Sprite class, we can use only some of the frames or create an animation by playing a single sequence. The Sprite class can also check if it is coincident with other Sprite classes or TiledLayers.

Tiledlayer and Sprite are a bit similar, but it is more used to create background, such as track or other larger areas. Tiledlayer contains a form (a Grid of Cells), we can use images or text to fill him. So a background or a scenario can be created with a series of small pictures.

HANDLING USER INPUT

In MIDP 2.0, the transfer user's input is different from the MIDP 1.0. You need to get the user's button in the game in 1.0. In 2.0, you can call GAMecanvas's getKeyStates () method to get the status of the keyboard directly.

Here is an example code. First we get the status of the keyboard, then make a corresponding response after judging the state of the arrow key.

protected void keypressed (int keycode) {

INT MOVE = 0;

INT KeyState = getKeyStates ();

IF (KeyState & Left_Pressed)! = 0) {

// do something

}

IF (KeyState & Right_Pressed)! = 0) {

// do something

}

IF (KeyState & Up_pressed)! = 0) {// do something

}

IF (KeyState & Down_Pressed)! = 0) {

// do something

}

}

Use screen cache

The screen cache enables the user to create a flashless animation, and do not need to create additional classes to implement dual buffers. The object is drawn to the cache first, ready to refresh to the screen.

In the following code, GAMECANVAS's getGraphics () is used to a display cache. In the While loop, buffer is used to draw the component of the LayerManager (Layers Object). Then the cache is refreshed by the FlushGraphics () method. Calling FlushGraphics (int X, int y, int width) method can refresh the specified area on the screen.

Public void run () {

Graphics g = getGraphics ();

While (play) {

Try {

// First Draw All the Layers

Layers.paint (g, 0, 0);

// if the game is on, flush the graphics

IF (play) {

Flushgraphics ();

}

Try {

Mythread.sleep (SleepTime);

} catch (java.lang.interruptedException e) {}

} catch (exception e) {

E.PrintStackTrace ();

}

}

}

Layout

In a game (or other graphics program), the display area typically contains different content (the image may be associated or is not associated). For example, a bee can fly on the forest, land, and water, but people in a maze cannot cross the wall.

MIDP 2.0 Game API introduces the layer. The layer provides a method of controlling an object or context on the screen. The layer can be Tiledlayer, Sprite, or by inheriting the Layer class-defined class.

The following code is an example of using the layer, the most important classes in this example are Tiledlayer, LayerManager, and Image. Image is used to hold images or image elements with the same size. Tiledlayer uses these images to arrange background

When the instance of TileDlayer is created, the constructor requires five parameters, columns, rows, images, and widths and heights of image elements. The background table here contains 40 lines, 16 columns, and the tiles.png and long width are 7. Some constants at the beginning of the code (tile_ground etc.) Represent references to image elements.

When the TileDlayer instance is created, the table of image elements can be filled with a Fillcells () method to populate or fillcell (). The last TiledLayer of the code is added to the LayerManager. Append () method is used to join the layer to the bottom of the observation window. Use INSERT () to insert the layer into the specified location.

Private tiledlayer tiles;

PRIVATE LAYERMANAGER LAYERS;

Private image tilesimage;

Public final int tile_ground = 1;

Public final int Tile_water = 2;

Public final INT TILE_SHORE_LEFT = 3;

Public final INT TILE_FOREST = 4;

Public final int Tile_Air = 5; public final int Tile_shore_right = 5;

// ...

// Creating An Instance of the Tiledlayer

Layers = New LayerManager ();

Try {

TILESIMAGE = image.createImage ("/ tiles.png");

} catch (ioexception e) {}

TILES = New Tiledlayer (40, 16, TilesImage, 7, 7);

// ...

// filling the tiledlayer with tiles

Tiles.Fillcells (0, 0, 40, 16, tile_air);

Tiles.Fillcells (14, 12, 12, 4, tile_water);

Tiles.Fillcells (0, 10, 14, 6, tile_ground);

// and more tiles like forest and the shores ...

Layers.Append (tiles);

The effect is as follows.

Elf

As mentioned earlier, the elf is defined as a separate object on the screen. This object can be a small person pushing the stone, a plane being shooting. The working way of the Sprite class is a bit similar to TilesLayer (both inheritance from the Layer class). Sprite also has an Image object that contains several sizes of images. However, these images are different from image elements that make up the background, they are the frames of the animation of the game protagonist. Therefore, the wizard can have an animation effect and easily change the image of the elves by replacing some of the frames.

The following code shows how to create an instance of Sprite. The principle is actually the same as TileDlayer.

Try {

SpriteImage = image.createImage ("/ sprite.png");

} catch (ioexception e) {}

Sprite = New Sprite (SpriteImage, 7, 7);

The following two methods in the Layer class can easily control the movement of the wizard:

Move (int DX, int DY)

SetPositions (int x, int y)

Controlling the movement of the wizard through the LayerManager, drawing makes it very convenient. The following code shows how to control the movement of a wizard. The elf is 7 × 7, and the magnitude of each move is also 7 pixels.

Public static final int upd = 0;

Public static final int right = 1;

Public static final int down = 2;

Public static final int LEFT = 3;

// ...

Switch (direction) {

Case Up:

Sprite.move (0, -7);

Break;

Case Down:

Sprite.move (0, 7);

Break;

Case Right:

Sprite.move (7, 0);

Break;

Case LEFT:

Sprite.move (-7, 0);

Break;

DEFAULT: BREAK;

}

There is also an important task in the game writing to find the collision between the eliance. Elf may have to stay in a game area or a designated maze, and it is very important to judge mutual collision between elves. The collision in some game means the direction of conversion, sometimes it means Game over

Sprite offers the following four ways to make us a judgment of the elves:

Collideswith (Image Image, Int X, Int Y,

Boolean Pixellevel)

Collideswith (Sprite S, Boolean Pixellevel) Collideswith (Tiledlayer T, Boolean Pixelled)

DefineCollisionRectangle (int X, int y,

Int Width, int Height

When two sprite instances (or when tiledlayer, sprite, image) collision or coincidence, we can respond to this.

Conclude

In combination with several examples, the new features of the most common game API in MIDP 2.0 are introduced. With these features, developers can not only control the drawings of the screen larger than the screen, and can be easily drawn on different layers. .

转载请注明原文地址:https://www.9cbs.com/read-29643.html

New Post(0)