Sprite, elves, as the name, dedicated to represent animation roles in the game, such as aircraft, tanks, etc. In MIDP1.0, we must write a special class to implement Sprite. Fortunately, MIDP2.0 provides powerful support for Sprite, you can create static, dynamic, opaque and transparent Sprite, below we are ready at the last time GameCanvas adds a sprite and let it move.
The main constructors of Sprite are:
Sprite (image): Constructs a single pattern of sprite;
Sprite (Image, Int Width, Int Height): Constructs an animation sprite, the image will be divided into n frame according to the specified size, allowing the sprite to move by SetFrame (int index). We used a PNG picture with a transparent background to create a tank's sprite:
(Note that this figure is an enlarged JPG format, you need to use the software such as Photoshop to process the PNG format with a transparent background, size is 64x16)
We have established the following projects and directories in Eclipse:
The following is a TankGamecanvas.java that draws sprite:
package tank.midp.core; import javax.microedition.lcdui *; import javax.microedition.lcdui.game *; public class TankGameCanvas extends GameCanvas implements Runnable {// direction control:.. private static int INDEX_OF_UP = 0; private static int INDEX_OF_DOWN = 1; private static int INDEX_OF_LEFT = 3; private static int INDEX_OF_RIGHT = 2; 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 private Sprite spriteTank;! // our sprite // Constructor and initialization public TankGameCanvas () {super (true ); Width = getWidth (); height = getHeight (); currentx = width / 2; currenty = height / 2; delay = 20; // init sprite: try {image image = image.createImage ("/ RES / IMG / Player1.png "); // Path SpriteTank = New Sprite (Image, 16, 16); // Size is 16x16} catch (Exception E) {E.PrintStackTrace ();}} // Automatical Start Thread for Game loop public void start () {isplay = true New thread (this) .start (); public void stop () {isplay = false;} // main game loop public void run () {graphics g = getGraph (); while (isplay) {input (); DrawScreen (g); try {thread.sleep (delay);} catch (interruptedException IE) {}}} // method to handle user input private void infut () {int keyStates =
getKeyStates (); // Left if ((KeyStates & Left_PRESSED)! = 0) {currentx = Math.max (0, Currentx - 1); spritetank.setframe (index_of_left);} // Right IF ((KeyStates & Right_Pressed) ! = 0) {IF (CurrentX 5 What do you need to pay attention to: MIDP's class needs preverify to run, so the executable class is in the verified directory instead of the bin directory, and you need to copy the entire RES directory to Verified / Classes before running. If you re-compile the code, the verified directory will be emptied first, only classes left, every time you have a copy this RES directory is very unhappy, we are ready to write an Ant script to let it automate all work. : ~ ~