In the concept of the use layer in J2ME game development, how to use the layer in game development, which mentions a concept of LayerManager, and this article will use this concept to implement the screen scrolling function.
The moving effect of the screen is usually implemented by changing the location of View Window. For example, you want the screen to move right, then you want to adjust the X coordinates of View Window to increase the corresponding value, if you want the screen to move left, then adjust the view WINDOW's X coordinates reduce the corresponding value. Like the principle of moving up and down. After getting the user's input, we can adjust the location of View Window and re-draw the screen. PRIVATE VOID INPUT () {Int KeyStates = getKeyStates ();
IF (KeyStates & Left_PRESSED! = 0) {IF (SCNX - 1> 0) SCNX -;} if (KeyStates & Right_Pressed)! = 0) {IF (SCNX 1 140 //Method to Display Graphics Private Void DrawScreen (Graphics G) { G.SetColor (0xfffffff); G.FillRect (0, 0, getWidth (), getHeight ()); g.setcolor (0x0000FF); // Display All Layers LayerManager.SetViewWindow (SCNX, SCNY, 140, 140); LayerManager.Paint (G, 20, 20); Flushgraphics ();} We only use one background image as follows: Since the program is relatively simple, it will not be explained directly from the source code. 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 width; // To hold screen width private int height; // To hold screen Height Private Int SCN, Scny; // To Hold Screen Starting ViewPoint // Sprites to be used Image BackgroundImage; Private Sprite Backgroundsprite; // Layer Manager Private LayerManager LayerManager; // Constructor and initialization public exampleGamecanvas () throws exception {super (true); width = getWidth (); height = getHeight (); SCNX = 55; scNy = 20; delay = 20; // load images = image.createImage ("/ background.png"); Backgroundsprite = New Sprite (BackgroundImage); LayerManager = new layermanager (); 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 input (int keystates = getKeyStates); IF (KeyStates & Left_PRESSED! = 0) {IF (SCNX - 1> 0) SCNX -;} if (KeyStates & Right_Pressed)! = 0) {IF (SCNX 1 140 //Method to Display Graphics Private Void DrawScreen (Graphics G) { G.SetColor (0xfffffff); G.FillRect (0, 0, getWidth (), getHeight ()); g.setcolor (0x0000FF); // Display All Layers LayerManager.SetViewWindow (SCNX, SCNY, 140, 140); LayerManager.Paint (G, 20, 20); Flushgraphics (); } Import javax.microedition.midlet. *; import javax.microedition.lcdui. *; public class simplescrollinglayermanger extends MIDlet {private 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 Display getDisplay () {Return Display; Public void pauseapp () {} Public void destroyApp (boolean unconditional) {exit ();} Public void exit () {system.gc (); destroyApp (false); notifydestroyed ();}}