The concept of the layer used in the development of games based on

Source: Internet
Author: User
Midp2.0 provides javax. microedition. lcdui. game package, which makes it easier for us to develop games. This package contains five classes: gamecanvas, layer, layermanager, titledlayder, and Sprite. Layer is an abstract class. Both tiledlayer and sprite are subclasses of layer. The former is used to draw scenes, and the latter is used to draw game roles. Layermanager is the management layer. This article uses a simple example to describe how to use a layer.

We can think of the game space as three-dimensional. What we see in our eyes is the two-dimensional space of x-y, which actually exists in the third-layer. Layermanager is responsible for managing these layers. In the order of addition, layermanager maintains layer indexes. The index of the first layer added is 0, the index of the second layer is 2, and so on. If we delete or add a layer, the index will be automatically adjusted. This is different from the concept of ID in RMS. ID is not an index, but 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 to the specified index location through insert (layer L, int index. To delete a layer, you only need to remove (layer L ). An important concept in layermanager is View window, which controls the visible area of the user. Its position is relative to the coordinate system of layermanager. By changing the position of the visual window, we can make the screen scroll effect, which will be introduced later in the article. By using setviewwindow (int x, int y, int width, int height), we can set the position and size of the visible window. For example

 

 

 

 

 

 

 

 

 

By calling setviewwindow (,), we can display an area with a size of 85*85 and A coordinate of () relative to layermanager to the user. For specific definitions, see Java Doc.

The following example demonstrates how to use a layer. We prepare two images, representing two layers. As follows:

 

We need to display the above two figures on the screen as needed, and control the movements of the genie by pressing the buttons. At this time, we use the sprite class, through which we can easily produce multi-frame effects. ConstructorSprite(Image, int framewidth, int frameheight) You can divide a specified image by the specified framewidth and frameheight. For example, you can get 5 frames through the following operations, frames are counted from 0. The split sequence is from left to right and from top to bottom.
Image playerimage = image. createimage ("/transparent.png ");
Playersprite = new sprite (playerimage, 32, 32 );
System. Out. println (playersprite. getrawframecount ());
You can call the setframe (INT index) method to select the specified frame. When the method paint (Graphics g) is called, the current frame is drawn.

The source code of the program is given below.

Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;

Public class examplelayermanagermidlet extends MIDlet
{
Private 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 );
Yydestroyed ();
}
}

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;

// Constructor and initialization
Public examplegamecanvas () throws exception
{
Super (true );
Width = getwidth ();
Height = getheight ();

Currentx = width/2;
Currenty = height/2;
Delay = 20;

// Load images to sprites
Image playerimage = image. createimage ("/transparent.png ");
Playersprite = new sprite (playerimage, 32, 32 );
System. Out. 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)
If (currentx + 5 <width)
{
Currentx = math. Min (width, currentx + 1 );
Playersprite. setframe (3 );
}

// Up
If (keystates & up_pressed )! = 0)
{
Currenty = math. Max (0, currenty-1 );
Playersprite. setframe (2 );
}

// Down
If (keystates & down_pressed )! = 0)
If (currenty + 10 {
Currenty = math. Min (height, currenty + 1 );
Playersprite. setframe (4 );
}
}

// Method to display graphics
Private void drawscreen (Graphics g)
{
G. setcolor (0 xffffff );
G. fillrect (0, 0, getwidth (), getheight ());
G. setcolor (0x0000ff );

// Updating player sprite position
Playersprite. setposition (currentx, currenty );

Layermanager. setviewwindow (55, 20,140,140 );
Layermanager. Paint (G, 20, 20 );

Flushgraphics ();
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.