站内搜索: 请输入搜索关键词
当前页面: 图书首页 > MIDP Style Guide for the Java 2 Platform, Micro Edition

MIDP Style Guide for the Java 2 Platform, Micro Edition

[ directory ] Previous Section Next Section

11.3 Layers

In addition to the game canvas, the game package provides layers, which are basic visual elements on a game screen. One type of layer is a sprite. Another type is a tiled layer, which enables an application developer to construct a single image from smaller individual images. The game package also provides a layer manager for applications that use multiple layers or that have layers larger than the device screen.

11.3.1 Sprites

A sprite is an image comprised of frames (smaller images). When an application draws a sprite, it draws one of the frames. An application can show different frames in a sequence to animate the sprite. The sprites in MIDP also provide transformations, such as mirroring, and collision detection.

Application Developer

Recommend: graphics/bulb2_icon.gif Use sprites for animation. Don't try to create your own animation code. The sprites in a MIDP implementation provide additional features, such as collision detection and transformations. They will also be tuned to perform well on their devices.

Sprites provide transformations such as rotating 90 degrees. They provide the concept of a reference pixel so that the transformations can appear to be taking place around a certain place in the sprite.

Strongly Recommend: graphics/bulb1_icon.gif Remember that the transformation of a sprite is around the reference pixel, not a reference point. A point is an (x,y) position on the canvas. A pixel is the pixel below and to the right of the point that defines it. (It is in the space defined by the points (x,y), (x+1,y), (x,y+1), (x+1,y+1).) (See Chapter 10 for more information.)

When an application developer positions a sprite at an initial x and y point, the point sets the location of the upper-left corner of the sprite's visual bounds. It puts the sprite's top, left pixel at the pixel below and to the right of the (x,y) position on the canvas, as shown in Figure 11.1.

Figure 11.1. Positioning a Sprite

graphics/11fig01.gif

By contrast, setting a reference around which to transform a sprite requires an application developer to provide an x and a y value, but the values define the position of a reference pixel, as shown in Figure 11.2, not a point.

Figure 11.2. A Sprite's Reference Pixel

graphics/11fig02.gif

Another feature of sprites is collision detection, which takes place within a collision rectangle. By default the rectangle is the entire sprite, but the application developer can make the rectangle smaller or larger. Collision detection checks for an intersection between the collision rectangle and an image, a nonempty tile in a tiled layer, or another sprite. Collision detection can also be done at the pixel level. In this case, the algorithm checks for collisions using only opaque pixels in the collision rectangle and the other tile, sprite, or image.

Application Developers

Strongly Recommend: graphics/bulb1_icon.gif Minimize your use of pixel-level collision detection. It may be less efficient to check for collisions at this level than for an intersection of the entities' boundaries.

11.3.2 Tiled Layers

Tiled layers provide an alternative to using large images in games. A tiled layer is a graphic made up of individual rectangles, and a group of small images that are the same size as the rectangles. The individual tiles are written into the rectangle in the order specified by the application developer. Figure 11.3 shows how tiles can be written to a tiled layer to form a larger picture.

Figure 11.3. Tiled Layer and Tiles

graphics/11fig03.gif

Using a few individual tiles over and over to form a picture allows a game to use fewer system resources than would be used by a single large graphic. Using tiles can minimize screen redrawing and require less storage space on the device than a large picture would.

Application Developers

Recommend: graphics/bulb2_icon.gif In order to minimize resource requirements and improve performance, use tiles whenever possible to create your background images. For instance, the Push Puzzle example with the MIDP Reference Implementation uses tiled layers. Each level is simply a different arrangement of the tiles (as shown in Figure 11.4), and each theme is a new set of tiles (as shown in Figure 11.5).

Figure 11.4. Different Tile Arrangements Form Different Game Layers

graphics/11fig04.gif

Figure 11.5. Different Tile Groups Form Different Themes

graphics/11fig05.gif

    [ directory ] Previous Section Next Section