0% found this document useful (0 votes)
280 views3 pages

Sprite in J2ME Game

This document provides an example of using sprites in a J2ME mobile game. It includes code for an ExampleGameCanvas class that implements a basic game loop to update and draw sprites on the screen. The code loads image files to create Sprite objects and updates their position and frame based on user input. The game canvas handles drawing the background, sprites, and flushing graphics. It also includes a GCanvas class that initializes the display and game canvas.

Uploaded by

Dong Van Hung
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
280 views3 pages

Sprite in J2ME Game

This document provides an example of using sprites in a J2ME mobile game. It includes code for an ExampleGameCanvas class that implements a basic game loop to update and draw sprites on the screen. The code loads image files to create Sprite objects and updates their position and frame based on user input. The game canvas handles drawing the background, sprites, and flushing graphics. It also includes a GCanvas class that initializes the display and game canvas.

Uploaded by

Dong Van Hung
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Sprite in J2ME Game

Saturday, 29. August 2009, 22:48:50

Sprite-Game-J2me
Một ví dụ đơn giản về cách dùng Sprite trong Mobile Game:
ExampleGameCanvas.java

Code:
package com.game;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
*
* @author SmallCode_ws
*/
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 sprite;
private Sprite nonTransparentSprite;

// Constructor and initialization


public ExampleGameCanvas() throws Exception {
super(true);
width = getWidth();
height = getHeight();
currentX = width / 2;
currentY = height / 2;
delay = 20;
try{
// Load Images to Sprites
Image image = Image.createImage("/1.png");
sprite = new Sprite (image,80,80);

Image imageTemp = Image.createImage("/2.png");


nonTransparentSprite = new Sprite (imageTemp,80,80);
}catch(Exception e){
System.out.println("Image Load error");
}
}
// 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();

sprite.setFrame(0);

// Left
if ((keyStates & LEFT_PRESSED) != 0) {
currentX = Math.max(0, currentX - 1);
sprite.setFrame(1);
}
// Right
if ((keyStates & RIGHT_PRESSED) !=0 )
if ( currentX + 5 < width) {
currentX = Math.min(width, currentX + 1);
sprite.setFrame(3);
}
// Up
if ((keyStates & UP_PRESSED) != 0) {
currentY = Math.max(0, currentY - 1);
sprite.setFrame(2);
}
// Down
if ((keyStates & DOWN_PRESSED) !=0)
if ( currentY + 10 < height) {
currentY = Math.min(height, currentY + 1);
sprite.setFrame(4);
}
}
// Method to Display Graphics
private void drawScreen(Graphics g) {
g.setColor(0xFF0000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);

// display sprites
sprite.setPosition(currentX,currentY);
sprite.paint(g);
nonTransparentSprite.paint(g);

flushGraphics();
}
}
GCanvas.java

Code:
package com.game;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* @author SmallCode_ws
*/
public class GCanvas 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();
}
}

You might also like