[Translation] Creating a 2D Action Game using the Game API (2)

zhaozj2021-02-16  87

(Connected to above)

The game scene is like the onion head

Typical 2D Action Games include a background and some active characters. Although you can draw such scenarios, the Game API can make you create a scene with Layers. You can use a Layer to make a city background, the other Do a car. Place the car above the background layer to complete the entire scene. By making a car as a layer of other layers in the background and scenes makes it simple.

Game API provides flexible support by the following four classes:

Layer is an abstract parent class of all layers. It defines the basic properties of the layer, including location, size, and whether the subclass of each Layer must define the Paint () method to draw this layer on Graphics. Its two Specific subclasses, TileDlayer and Sprite should meet the needs of 2D games.

TileDlayer is used when creating a background chart. You can use a group of small pieces of pictures to efficiently create large background diagrams.

Sprite is a moving layer. You provide a picture of each action (frame) and completely control it. Sprite also provides a function of mirror flipping and 90 ° multiplexing of each frame.

LayerManager is a very easy class that records all the layers in your scene. Simply call the LayerManager's Paint () method to draw all the layers it included.

Use tiledlayer

Tiledlayer is very simple, although it is somewhat in-depth place to understand. The most important idea is a picture source to provide a set of Tiles (slices) for forming a large scene. For example, the picture size below is 64 x 48.

Picture source

This picture can be divided into 12 slices, each size is 16 x 16 pixels. Tiledlayer assigns a number for each slice, starting from the upper left corner 1. The slice number in the image source is as follows:

Sliced ​​number

Coding to create a TiledLayer wants to be easy. You need to specify the number of rows and columns, photos, and the size of each slice. This code illustrates how to load the image source and create a TileDlayer.

Image image = image.createImage ("/ board.png");

Tiledlayer tiledlayer = new tiledlayer (10, 10, image, 16, 16);

In the example, the newly created TiledLayer has 10 rows of 10 columns. Each slice is 16 pixels square.

Part of the victory is how to create a scenario with these slices. To assign a slice for one of the scene in the scene, you should use the setcell () method. You need to provide a line and column position and the number of the slice. For example, you want to slice 5 Allocated to the lattice of the third line of Character 2, you should use SetCell (2, 1, 5). If you look at these parameters, please pay attention to the sliced ​​number starting from 1, while the lattice row and column number 0 Start. The default sliced ​​number of each grid in the new TileDlayer is 0, which indicates that they are empty.

The following code segment demonstrates a method of creating TiledLayer using an int array. In true games, TiledLayers should be defined by the resource file, which can be more flexibly define the background and to extend new maps or levels for the game.

Private tiledlayer createboard () {

Image image = NULL;

Try {image = image.createImage ("/ board.png");}

Catch (IOEXCEPTION IOE) {Return Null;}

Tiledlayer tiledlayer = new tiledlayer (10, 10, image, 16, 16);

int [] map = {

1, 1, 1, 1, 11, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 9, 0, 0, 0, 0, 0,

0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 0,

1, 1, 1, 1, 6, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 7, 11, 0,

0, 0, 0, 0, 0, 0, 7, 6, 0, 0,

0, 0, 0, 0, 0, 7, 6, 0, 0, 0

}

For (int i = 0; i

INT column = i% 10;

INT row = (i - column) / 10;

Tiledlayer.Setcell (Column, Row, Map [I]);

}

Return tiledlayer;

}

You need to get a Graphics object in a Paint () method to draw this tiledlayer on the screen.

TileDlayer also supports dynamic slices, which makes it easy to achieve in a certain order. More details, please refer to Tiledlayer's JavaDoc.

Implement the human animation with SPRITES

Another subclass of Layer, provided by the Game API is Sprite. In a sense, sprite is a antonym on the Tilelayer concept. TileDlayer uses a bunch of sections of the picture source to form a scene, and sprite is used in a photo source sequence. Implement animation.

All jobs for creating a Sprite need to provide a picture source and the picture size of each frame. Tiledlayer, the image source is a slice divided into the same size; Sprite, each sub-picture is called a frame. In the example, the picture source tank.png is used to create Sprite for each frame 32 x 32 pixel.

Private microtanksprite createtank () {

Image image = NULL;

Try {image = image.createImage ("/ tank.png");}

Catch (IOEXCEPTION IOE) {Return Null;}

Return New Microtanksprite (image, 32, 32);

}

Each frame of the picture source is numbered from 0, (here don't be halo; the slice number starts from 1.) Sprite has a frame sequence (frame sequence) to determine what order each frame is displayed. New The SPRITE default frame sequence is arranged in sequence from 0.

The front-rear transformation in the frame sequence should be used using the nextframe () and prevframe () method. These two methods will automatically determine the boundary processing. For example, if the Sprite is displaying the last frame in its frame sequence, call nextframe () The first frame of the frame sequence is displayed.

Specify different sequences with the default, represent the new sequence as an int array and passed to setframesequence ().

[不 不] You can Jump to a particular point in the current frame sequence by calling setframe (). There is no way to jump to a specific frame number. You can only jump to a certin point in the frame sequence ..

The change in the frame is only visible when the Sprite is next redrawn next time, that is, the PAINT () method inherited from the Layer class is called.

Sprite can also rotate the frame. Each frame can rotate the multiple of 90 °, perform mirror rotation, or the binding of both. The constant in the SPRITE class lists all possibilities. You can use the settransform () method to use these constants. The current rotation of Sprite. The following list uses the current frame to rotate and rotates 90 ° in the longitudinal center (did not see the effect, do not know the translation.): // sprite sprite = ...

Sprite.settransform (sprite.trans_mirror_rot90);

The image is rotated, but the Sprite's Reference Pixel is not moved. By default, the Sprite's reference point is at its 0,0, the coordinate is the upper left corner. When the image is rotated, the position of the reference point is also rotated, but Relative to the image, the location of the reference point is still at the place (too foot, what does it mean by understanding it).

You can use the defineReferencePixel () method to change the location of the reference point. In many animations you may need to set the reference point in the center of Sprite.

Finally, Sprite provides some collideswith () methods for collision detection between other sprites, tiledlayers, or images. You can use rectangular detection (convenient but not allowable) or on this level of pixels (trouble but accurate). The difference between these methods is not a two sentences to understand, you still go to Javadoc.

Clapton_XPathotmaildotcom

[Lazy behind, it is better to see the code directly]

The Mutank EXAMPLE

The Mutank Example Demonstrates The Use of Tiledlayer, Sprite, And LayerManager.

The Important Classes Area Microtankcanvas, Which Contains Most of The Code, And Microtanksprite, Which Encapsules The Behavior of The Tank.

MicroTankSprite makes extensive use of transformations. Using a source image with only three frames, MicroTankSprite can show the tank pointing in any of 16 different directions. Two exposed public methods, turn () and forward (), make the tank easy to control.

MicroTankCanvas is a GameCanvas subclass and contains an animation loop in run () that should look familiar to you. The tick () method checks to see if the tank has collided with the board. If so, its last movement is reversed using MicroTankSprite's undo ( ) method. The input () method simply checks for key presses and adjusts the direction or position of the tank accordingly. The render () method uses a LayerManager to handle painting. The LayerManager contains two layers, one for the tank, one for the Board.

The debug () method, called from the game loop, compares the elapsed time through the game loop with the desired loop time (80 milliseconds) and displays the percentage of time used on the screen. It is for diagnostic purposes only, and would be removed before the game was shipped to customers.The timing of the game loop is more sophisticated than in the previous SimpleGameCanvas. to try to perform one iteration of the game loop every 80 milliseconds accurately, MicroTankCanvas measures the time it takes to perform tick () , INPUT (), AND render (). It dams for the remain of the 80-millisecond cycle, Keeping The Total Time Through Each Loop As Close AS Possible To 80 MilliseConds.

Summary

MIDP 2.0's Game API provides a framework that simplifies developing 2D action games. First, the GameCanvas class provides painting and input methods that make a tight game loop possible. Next, a framework of layers makes it possible to create complex scenes. TiledLayer assembles a large background or scene from a palette of source image tiles. Sprite is appropriate for animated characters and can detect collisions with other objects in the game. LayerManager is the glue that holds layers together. The muTank example provides a foundation of working code to demonstrate The Game API.

About the Author: Jonathan Knudsen [e-mail] [home page] is the author of several books, including Wireless Java (second edition), The Unofficial Guide to LEGO MINDSTORMS Robots, Learning Java (second edition), and Java 2D Graphics. Jonathan Has Written Extensively About Java and Lego Robots, Including Articles For JavaWorld, Exe, Nzz Folio, and The O'Reilly Network. Jonathan Holds a Degree In Mechanical Engineering from

PRINCETON

University

.

Clapton_XPathotmaildotcom

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

New Post(0)