J2ME game development

xiaoxiao2021-03-06  90

Javax.microedition.lcdui.game package is available in MIDP 2.0 so that we can make game development, including 5 classes in this package, named Gamecanvas, Layer, LayerManager, Titledlayder, and Sprite. Layer is an abstract class, Tiledlayer and Sprite are subclasses of Layer, the former is for the rear of the scene for painting games. LayerManager is a management. This article describes how to use a layer through a simple example.

We can imagine the space of the game into three dimensions, our eyes see the 2-dimensional space of X-Y, in fact, there is still a third dimension-layer. LayerManager is responsible for managing these layers, and maintains the index of the layer according to the order of adding, the index of the first added layer is 0, and the second addition layer index is 2, and it is pushed. If we delete or add a layer, then the index will automatically adjust. The concept of this and the ID in the RMS is different, ID is not an index, just a simple tag record. When we want to add a layer, we only need to call the method append (Layer L), of course, you can also insert the layer into the specified index location by INSERT (Layer L, INDEX). The deletion layer only needs REMOVE (Layer L). An important concept in LayerManager is View Window, View Window controls the user-visible area, his location is relative to the coordinate system of LayerManager. By changing the location of the visible window, we can make the effect of screen scrolling, which will be introduced later. Through method setViewWindow (int X, int y, int width, we can set the location and size of the visible window. E.g

By calling methods setViewWindow (52, 11, 85, 85) we can display a region of 85 * 85, the coordinates of the LayerManager (52, 11) relative to the LayerManager. Please refer to Java DOC for specific definitions.

Below we demonstrate how to use the concept of layers. We prepare two pictures, represent two layers, respectively. as follows

We have to implement the above two figures on the screen, and can control the movement of the elves by pressing the button. At this time we use the Sprite class, through it we can easily produce multi-frame effects. The constructor sprite (image image, int framewidth) can split the specified picture image in accordance with the specified framewidth and frameheHEHT, such as the above, 5 frames can be obtained by the following operation, the frame starts from 0, segmentation The order is from left to right, from top to bottom. Image playerImage = Image.createImage ( "/ transparent.png"); playerSprite = new Sprite (playerImage, 32,32); System.out.println (playerSprite.getRawFrameCount ()); we can call setFrame (int index) method Select the specified frame that the current frame will be drawn when the method Paint (Graphics G) is called.

The source code for the program will be given below. Import javax.microedition.midlet. *; import javax.microedition.lcdui. *;

Public Class ExampleLayerManagermidlet Extends MIDlet {Private Display Display DISPLAY DISPLAY

public void startApp () {try {display = Display.getDisplay (this); ExampleGameCanvas gameCanvas = new ExampleGameCanvas (); gameCanvas.start (); display.setCurrent (gameCanvas);} catch (Exception ex) {System.out.println (EX);

Public void pauseapp () {}

Public void destroyApp (boolean unconditional) {exit ();}

Public void exit () {system.gc (); destroyApp (false); notifydestroyed ();}}

import javax.microedition.lcdui *;. import javax.microedition.lcdui.game *;. public class ExampleGameCanvas extends GameCanvas implements Runnable {private boolean isPlay; // Game Loop runs when isPlay is true private long delay; // To give thread consistency private int currentX, currentY; // to hold current position of the 'X' private int width; // to hold screen width private int height; // to hold screen height // Sprites to be used private Sprite playerSprite; private Sprite backgroundSprite; // Layer Manager private LayerManager layerManager; // Constructor and initialization public ExampleGameCanvas () throws Exception {super (true); width = getWidth (); height = getHeight (); currentX = width / 2; currentY = height / 2 Delay = 20; // load images = image.createImage ("/ transparent.png"); Playersprite = New Sprite (PlayerImage, 32, 32); System.ou t.println (playerSprite.getRawFrameCount ()); Image backgroundImage = Image.createImage ( "/ background.png"); backgroundSprite = new Sprite (backgroundImage); layerManager = new LayerManager (); layerManager.append (playerSprite); layerManager. Append (Backgroundsprite);} // Automatically Start Thread for Game Loop Public Void Start (isplay = true; thread t = new thread (this); t.start ();} public void stop () {isplay = false; } // main game loop public void run () {graphics g = getGraphics (); while (isplay == true) {input ();

drawScreen (g); try {Thread.sleep (delay);} catch (InterruptedException ie) {}}} // Method to Handle User Inputs private void input () {int keyStates = getKeyStates (); playerSprite.setFrame (0) ; // Left if (KeyStates & Left_PRESSED! = 0) {currentx = Math.max (0, Currentx - 1); Playersprite.SetFrame (1);} // Right IF ((KeyStates & Right_Pressed)! = 0! ) F (currentx 5

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

New Post(0)