Mobile gaming has got very popular these days. It has in fact become the bread and butter for most mobile software companies. Also it is next big thing according to industry analysts. But in spite of all the hype the first generation MIDP platform was not having a direct support for mobile gaming. We as developers had to write our own game engines. This presented a very big barrier to the creativity of game developers. The look and feel of the game was highly correlated with the quality of game engine used. Since there were no standard game engines available every company developed its own game engine thereby increasing the development costs and time. Also since writing a game engine is not an easy task most software professionals that tried to enter this line without any support had to leave game development. Keeping this in mind the developers of the MIDP 2.0 have given us a standard game engine. They have given us some standard classes that could make the development of 2D games a lot easier. In fact I can personally vouch that making a game in MIDP 2.0 is at least 70% simpler than making the same game in MIDP 1.0. javax.microedition.lcdui.game The game development classes in MIDP 2.0 are contained in this package. This package contains the following classes for simplifying the task of game creation: · GameCanvas · Layer · LayerManager · Sprite · TiledLayer Each of these classes has been explained below in detail. public abstract class GameCanvas What is it? The GameCanvas class is a very important new addition in MIDP 2.0. It provides the basis for a full fledged game user interface. The earlier Canvas class in MIDP 1.0 had certain shortcomings with respect to gaming. This class inherits from Canvas the functionality related to commands, input events etc. It also provides game-specific capabilities such as an off-screen graphics buffer and the ability to query key status. o Constructors
This constructor creates a new instance of a GameCanvas. This constructor also creates a new buffer for the GameCanvas which is initially filled with white pixels. o Fields
This bit represents the DOWN key. Constant Value: 0x0040
This bit represents the FIRE key. Constant Value: 0x0100
This bit represents the GAME_A key (This key may not be supported on all devices). Constant Value: 0x0200
This bit represents the GAME_B key (This key may not be supported on all devices). Constant Value: 0x0400
This bit represents the GAME_C key (This key may not be supported on all devices). Constant Value: 0x0800
This bit represents the GAME_D key (This key may not be supported on all devices). Constant Value: 0x1000
This bit represents the LEFT key. Constant Value: 0x0004
This bit represents the RIGHT key. Constant Value: 0x0020
This bit represents the UP key. Constant Value: 0x0002 o Methods
This method flushes the off-screen buffer to the display. Size of the flushed area is equal to the size of this GameCanvas. This method does not change the contents of the off-screen buffer.
This method flushes the specified region of the off-screen buffer to the display. This method does not change the contents of the off-screen buffer.
This method returns the Graphics object that renders to this GameCanvas’ off-screen buffer.
This method returns an integer containing the key state information (one bit per key). This method return’s 0 if this GameCanvas is not currently shown.
This method paints this GameCanvas. public abstract class Layer What is it? The Layer is an abstract class. It represents a visual element of the game. Each Layer has certain properties like:
The subclasses of the Layer class must implement the paint method so that they can be rendered. o Methods
This method returns the height of the Layer object in pixels.
This method returns the width of the Layer object in pixels
This method returns the Layer object’s horizontal position.
This method returns the Layer object’s vertical position.
This method returns true if the Layer is visible. Otherwise it will return false when the Layer is invisible.
This method moves this Layer object by the specified horizontal and vertical distances.
This method paints this Layer if it is visible. The Layer object is rendered at its current x and y position on the screen. For example if the position of the Layer object is at x: 100, y: 100 then this object will be rendered starting at this position.
This method sets this Layer’s position such that its top-left corner is located at (x,y) position in the painter’s coordinate system.
This method sets the visibility of this Layer. A visible Layer is rendered when its paint method is called while an invisible Layer is not rendered. public class LayerManager What is it? The LayerManager class is used for managing a series of Layers. It is not that lLayers can not be used without this class but the LayerManager class simplifies the process of rendering the Layers which have been added to it by automatically rendering the correct regions of each Layer in the appropriate order. In fact LayerManager is one of the most powerful new features of MIDP 2.0. Keep in mind that the LayerManager paints the Layers in z – order i.e. the one appended last is painted first and the one appended first is painted the last.
o Constructors
This constructor creates a new LayerManager. o Methods
This method appends a Layer to this LayerManager. The Layer is appended to the list of existing Layers such that it has the highest index i.e. will be painted the furthest away from the user.
This method returns the Layer that has the specified index.
This method returns the number of Layers
This method inserts a new Layer in this LayerManager at the specified index. The Layer is first removed from this LayerManager if it has already been added.
This method renders the LayerManager’s current view window at the specified location.
This method removes the specified Layer from this LayerManager.
This method sets the view window on the LayerManager. The view window defines the region that the LayerManager draws when its paint method is called. This method allows the developer to control the size of the visible region. It also allows him to control the location as well of the view window relative to the LayerManager’s coordinate system. public class Sprite What is it? Games are generally composed of the characters and objects. Sprite is an element that can be rendered to show different characters and objects in the game. MIDP 2.0 provides a very versatile Sprite class. This class supports the following features: · Animations · Movement · Collision Detection · Image Transformations · Ability to Change The Visibility Status The Sprite class is thus an important class while constructing any animated game. This class is particularly useful for arcade game and side scrolling games. o Constructors
This constructor constructs a new non-animated Sprite using the Image in the parameter.
This constructor creates new animated Sprite using frames contained in the provided Image. All the frames in the image provided should be of equal size. The frameWidth and frameHeight parameters define the size of the frames.
This constructor creates a new Sprite from another Sprite. o Fields
This field causes the Sprite to appear reflected about its vertical center. Value: 2
This field causes the Sprite to appear reflected about its vertical center and then rotated clockwise by 180 degrees. Value: 1
This field causes the Sprite to appear reflected about its vertical center and then rotated clockwise by 270 degrees. Value: 4
This field causes the Sprite to appear reflected about its vertical center and then rotated clockwise by 90 degrees. Value: 7
This field tells that no transformation should be applied to the Sprite. Value: 0
This field causes the Sprite to appear rotated clockwise by 180 degrees. Value: 3
This field causes the Sprite to appear rotated clockwise by 270 degrees. Value: 6
This field causes the Sprite to appear rotated clockwise by 90 degrees. Value: 5 o Methods
This method returns true if this Sprite has collided with the Image else it will return false. If the pixelLevel parameter is true then the collision is checked for pixel by pixel. If the pixelLevel parameter is false then bounding box collision is used.
This method returns true if the two Sprites have collided else it will return false. If the pixelLevel parameter is true then the collision is checked for pixel by pixel. If the pixelLevel parameter is false then bounding box collision is used.
This method returns true if this Sprite has collided with the TiledLayer else it will return false. If the pixelLevel parameter is true then the collision is checked for pixel by pixel. If the pixelLevel parameter is false then bounding box collision is used. NOTE In pixel by pixel collision the transparent pixels are not taken into account. Thus the collisions look very real. In bounding box collision the user may some times see collision even when the actual images have not collided because the colliding region may be having transparent pixels or have pixels that merge with the background color.
This method defines the Sprite’s bounding rectangle that is used for collision detection purposes in bounding box collision. The x and y parameters in this method define the co-ordinate relative to the untransformed sprites top left edge.
This method defines the reference pixel for this Sprite. The x and y parameters in this method define the co-ordinate relative to the untransformed sprites top left edge. It may lie outside of the frame’s bounds.
This method returns the current index of the current frame of the sprite.
This method returns the number of elements in this Sprite’s frame sequence.
This method returns the number of raw frames for this Sprite.
This method returns the horizontal location of the reference pixel
This method returns the vertical location of the reference pixel
This method selects the next frame in the frame sequence.
This method draws the Sprite.
This method selects the previous frame in the frame sequence.
This method selects the current frame in the frame sequence.
This method is used for setting the frame sequence for this Sprite.
This method is used for changing the Image containing the Sprite’s frames.
This method sets this Sprite’s position such that its reference pixel is located at x and y positions in the painter’s coordinate system.
This method sets the transform type for this Sprite. Transformations can be applied to a Sprite to change its rendered appearance. public class TiledLayer What is it? Games with scrolling backgrounds are very popular. But in MIDP 1.0 creating such backgrounds with the use of repetitive tiles to save space was very difficult. Therefore in MIDP 2.0 the TiledLayer element has been added. It is a visual element composed of a grid of cells that can be filled with a set of tile images. This class allows large virtual layers to be created with just a few tiles. This technique is not a new technique. In fact I have known this technique from over a decade. What is new is its support in the mobile phone development environment. In fact in the mobile development environment this class is essential because of the limited memory and computational resources available to us for creating the games. TiledLayer can be though of as a grid of equal sized cells much like MS Excel. Each cell can have a particular tile index which determines the rendering of that cell. A TiledLayer can be bigger in size than the mobile phone screen. Also MIDP 2.0 supports animated tiles. o Constructors
This constructor constructs a new TiledLayer. The first two parameters determine the number of columns and rows in the TiledLayer. The image parameter determines the image containing tiles. The last parameters determine the size of the individual tiles in the TiledLayer. o Methods
This method returns the index of newly created animated tile. The index for the animated tiles are always negative.
This method fills a region cells with the specific tile. The tile index may refer to a static tile index, an animated tile or it may even be intentionally left blank. In fact in complex games with different superimposed TiledLayer objects leaving bank tiles is some time a necessity.
This method gets the tile referenced by an animated tile.
This method returns the index of tile in cell.
This method returns the height in pixels of a single cell in the TiledLayer grid.
This method returns the width in pixels of a single cell in the TiledLayer grid.
This method returns the width in columns of the TiledLayer grid.
This method returns the height in rows of the TiledLayer grid.
This method draws the TiledLayer. It is rendered subject to the clip region of the Graphics object.
This method associates an animated tile with the specified static tile.
This method sets the contents of a particular cell.
|
Mobile Technology > Java ME (J2ME) > Book - Mobile Phone Programming using Java ME (J2ME) > UNIT III >