[Translation] Game API to create 2-D action games (2)
(Connected to the text)
Scenes like the first game onion
Typical 2-D action game, including a background and some of the activities of the characters. Although you can draw their own to such scenes, but Game API allows you to create scenes with layers you can do with a layer of the urban background, and the other to do a car. put the car layer above the background layer to complete the whole scene. will be independent of the vehicle as a background scene and the other layers in the layer will become its operation simple.
Game API adoption of the following four categories providing a flexible layer of support:
- Layer is the father of all of the abstract category. Its definition of the basic attributes, including the location, size and whether visible. Layer each subclass must be defined paint () method used in the Graphics Rendering on the floor. Its two specific sub-categories, TiledLayer and Sprite, it will meet the needs of the 2-D games.
- TiledLayer background in the creation of maps will be used, you can use a group of small pieces of the picture to create an efficient substantial background.
- Sprite will be active layer. Provide you with every movement (frame) then the picture of the activities of its complete control. Sprite also provide each frame image flip and 90 ° rotating multiple functions.
- LayerManager is a very comfortable class, it records all of your scenes in the layer. LayerManager only call the paint () method can be drawn for all of it.
Use TiledLayer
TiledLayer is very simple, at first glance, although some in-depth understanding of the local poor. Ideology is the main source of a picture with a set of tiles (biopsy) for the formation of a big scene, for example, the following picture size of 64 x 48 .

Photo source
This picture can be divided into 12 sections, each the size of 16 x 16 pixels. TiledLayer for each section assigned a number, starting from the upper left corner to 1. Picture of the source code sections as follows:

Slice #
Coding is to create a TiledLayer want to be easy. You need to specify the number of columns and rows, the picture source, as well as the size of each slice. This code explains how to load your pictures and create a source TiledLayer.
Image image = Image.createImage ( "/ board.png");
TiledLayer tiledLayer = new TiledLayer (10, 10, image, 16, 16);
In the example, the newly created TiledLayer 10 to 10. Size of each slice is 16 pixels square.
Interesting part is how to use these sections to create scenes. Order to the scene of a grid distribution of a biopsy should be used setCell () method. You need to provide the rows and columns grid position and the number of sections, for example, you want to slice 5 allocated to the second line of the first three lattice, we should use setCell (2, 1, 5) If you see this several parameters uncomfortable, please note from the No. 1 slice, and the lattice from the rows and columns # 0 start. new TiledLayer each grid section of the default # 0, indicating that they are empty.
The following code demonstrates a use of the array create TiledLayer int method in the real game, TiledLayers should be defined by the resource file, so can be more flexible definition of the context of the game as well as expansion or new map level, etc..
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 <map.length; i + + i + +) (
Int column i = 10%;
Int row = (i - column) / 10;
TiledLayer.setCell (column, row, map [i]);
)
Return tiledLayer;
)
You need to paint () method to obtain this Graphics object TiledLayer painting on the screen.
TiledLayer also support dynamic slices, which makes a certain amount of sequence changes in a group of sections become very easy to achieve. TiledLayer more details please refer to the JavaDoc.
Sprites Sprites with achieving character animation
Game API provided by Layer Another category is the son of Sprite. A certain sense, Sprite TileLayer concept is the opposite. TiledLayer picture with the source of a lot of slices to a scene, and Sprite is the source of a picture sequence to achieve animation.
Create a Sprite all need to provide a picture of each frame and the source (frame) of the picture size. TiledLayer, the picture is the source of the same size was divided into sections; Sprite, each picture was known as a sub-frame. Below example, picture source tank.png be used to create each frame size of 32 x 32 pixels Sprite.
Private MicroTankSprite createTank () (
Image image = null;
Try (image = Image.createImage ( "/ tank.png");)
Catch (IOException ioe) (return null;)
Return new MicroTankSprite (image, 32, 32);
)
The source of each picture frame by a number from 0 Number sequence. (Here other halo; Slice # 1 from the beginning.) Sprite have a frame sequence (frame sequence) for a decision on what order each frame to display. New Sprite's default frame sequence starts with 0 ranked all frames.
In the frame in the sequence before and after the transformation should be used Sprite nextFrame () and prevFrame () method. These two methods will be automatically judged boundary treatment, for example, if Sprite that it is the sequence in the final frame one, calling nextFrame () frame sequence will show the first frame.
Designated with a different default sequence, the sequence that will be new to int array and passed to setFrameSequence ().
[Understand] 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 certain point in the frame sequence ..
Changes in the frame only redraw Sprite next time, that is inherited from the Layer of the paint () method is called before that time.
Sprite can also frame rotation changes. Each frame can be in multiples of 90 ° rotation, rotating mirror, or a combination of both. Constants in the Sprite class lists all possibilities. These constants can be used setTransform () method Sprite set the current rotation. Liezi below the current frame to do longitudinal centre axle rotating mirror rotation and 90 ° (not seen the results do not know the correct translation):
/ / Sprite sprite = …
Sprite.setTransform (Sprite.TRANS_MIRROR_ROT90);
Image rotation of the Sprite but the reference pixel (reference point) is not mobile. Default, Sprite reference point in the 0,0 coordinates is the upper left corner. When the image rotation, the reference point with the location change, but Comparing with the image point of reference for the location of the original is still (too bad, in the end fail to understand what is the meaning).
You can use defineReferencePixel () method to change the location of the reference point in many animation you may need to reference point located in the centre of sprite.
Finally, Sprite provide some collidesWith () methods are used with other Sprites, TiledLayers, or Images of the collision detection between you can use rectangular detection (but not easy) or in the pixel level to detect this (trouble but accurate) . these methods is not a difference between the two is right to understand, you had to see it JavaDoc.
[Bother turned back, as a direct look at the code woven?]
The muTank Example
The muTank example demonstrates the use of TiledLayer, Sprite, and LayerManager.
The important classes are MicroTankCanvas, which contains most of the code, and MicroTankSprite, which encapsulates 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 then sleeps for the remainder 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
Clapton_xpAThotmailDOTcom
Tags: api








0 Comments to “[Translation] Game API to create 2-D action games (2)”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.