problem is with "mainscrn.png" image ( dimension 128 / 128), only this image is working and if i replace this with ny other (say 16/16 or ny other multiple of 16 or even other image ) , then its not working
error name is "uncaught exception java/lang/IndexOutOfBoundsException"
- Code: Select all
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 ;
currentY = height;
delay = 20;
// Load Images to Sprites
Image playerImage = Image.createImage("/mainscrn.png");
playerSprite = new Sprite (playerImage,32,32);
Image backgroundImage = Image.createImage("/GladiatorGallery2.jpg");
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);
//backgroundSprite.setFrame(3);
}
// Right
if ((keyStates & RIGHT_PRESSED) !=0 )
if ( currentX + 5 < width) {
currentX = Math.min(width, currentX + 1);
playerSprite.setFrame(0);
}
// Up
if ((keyStates & UP_PRESSED) != 0) {
currentY = Math.max(0, currentY - 1);
playerSprite.setFrame(1);
}
// Down
if ((keyStates & DOWN_PRESSED) !=0)
if ( currentY + 10 < height) {
currentY = Math.min(height, currentY + 1);
playerSprite.setFrame(2);
}
}
// Method to Display Graphics
//private void drawScreen(Graphics g) {
//g.setColor(0x00C000);
private void drawScreen(Graphics g) {
//g.setColor(0x00C000);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffff00);
// updating player sprite position
playerSprite.setPosition(currentX,currentY);
// display all layers
layerManager.paint(g,0,0);
flushGraphics();
}
}
