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 app developer to construct a single image from smaller individual images. The game package also provides a layer manager for apps that use multiple layers or that have layers larger than the device screen.

Sprites

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

app Developer

Recommend: Java 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: Java 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 for more information.)

When an app 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 Screenshot.

Screenshot Positioning a Sprite

Java graphics 11fig01.gif


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

Screenshot A Sprite's Reference Pixel

Java 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 app 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.

app Developers

Strongly Recommend: Java 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.

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 app developer. Screenshot shows how tiles can be written to a tiled layer to form a larger picture.

Screenshot Tiled Layer and Tiles

Java 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.

app Developers

Recommend: Java 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 Screenshot), and each theme is a new set of tiles (as shown in Screenshot).

Screenshot Different Tile Arrangements Form Different Game Layers

Java graphics 11fig04.gif


Screenshot Different Tile Groups Form Different Themes

Java graphics 11fig05.gif




   
Comments