Não entendo porque a colisão não está sendo detectada, o sprite do personagem atravessa os blocos do cenário. A idéia é ter duas variáveis para guardar a posição atual do sprite e duas para guardar a posição anterior do sprite. Então, quando a colisão é detectada, o sprite deveria voltar uma posição. Mas não é isso que acontece!
classe CharacterClass:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class CharacterClass extends Sprite {
private int posx, posy; // To hold current position of sprite.
private int lastx, lasty; // To hold the last x and y positions.
private int frameWidth, frameHeight; // To hold size of sprite frame or frames.
private int lives = 3;
public CharacterClass(Image image, int frameWidth, int frameHeight) throws Exception {
super(image, frameWidth, frameHeight);
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
}
public int getLives() {
return lives;
}
public void setLives(int lives) {
this.lives = lives;
}
public void startPosition() {
posx = 16;
posy = 16;
lastx = posx;
lasty = posy;
this.setPosition(posx, posy);
}
public void moveRight() {
lastx = posx;
posx++;
this.setPosition(posx, posy);
}
public void moveLeft() {
lastx = posx;
posx--;
this.setPosition(posx, posy);
}
public void moveUp() {
lasty = posy;
posy--;
this.setPosition(posx, posy);
}
public void moveDown() {
lasty = posy;
posy++;
this.setPosition(posx, posy);
}
public void setLastPosition() {
this.setPosition(lastx, lasty);
}
public void paintSprite(Graphics g) {
this.paint(g);
}
}
classe GameCanvas:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.media.*;
import java.io.InputStream;
public class GameCanvasClass extends GameCanvas implements Runnable {
private boolean playing;
// TiledLayer.
private TiledLayer tiledCenary;
// Layer.
private LayerManager layer1 = new LayerManager();
// Game settings;
public static int gameLevel = 0;
public static int MAX_ENEMIES = 4;
public static final int NUMBER_LEVELS = 10;
public static boolean[] doneBuildLevel = new boolean[10];
// Sprites.
private CharacterClass player;
private CharacterClass[] enemy = new CharacterClass[4];
// Sounds.
private Player playerSound;
// Thread.
private Thread thread;
/** Creates a new instance of GameCanvasClass */
public GameCanvasClass() throws Exception {
super(true);
for(int i = 0; i < 10; i++)
doneBuildLevel[i] = false;
Image image = Image.createImage("/bear.PNG");
player = new CharacterClass(image, 16, 16);
player.startPosition();
}
public void start() throws Exception {
playing = true;
thread = new Thread(this);
thread.start();
try {
InputStream is = getClass().getResourceAsStream("sound.mid");
playerSound = Manager.createPlayer(is, "audio/midi");
playerSound.realize();
playerSound.prefetch();
playerSound.start();
}
catch(Exception e) {
System.out.println(e);
}
}
public void run() {
try {
setupGameLevel(gameLevel);
}
catch(Exception e) {
e.printStackTrace();
}
Graphics g = getGraphics();
while(playing == true) {
drawGameScreen(g);
checkInput();
checkCollision();
try {
Thread.sleep(15);
}
catch(InterruptedException ie) {
}
}
}
public void stop() {
playing = false;
if(player.getLives() >= 1 )
playing = true;
else {
System.out.println("GAME OVER");
try {
if(playerSound != null) {
playerSound.stop();
playerSound.close();
playerSound = null;
}
}
catch(Exception e) {
System.out.println(e);
}
}
}
private void setupGameLevel(int level) throws Exception {
if( (level == 0) && (doneBuildLevel[level] == false) ) {
// Building cenary for the first level.
tiledCenary = TiledLayerClass.buildTiledCenary(gameLevel);
// Append to the layer.
layer1.append(player);
layer1.append(tiledCenary);
}
}
private void checkInput() {
int keyState = getKeyStates();
if( (keyState & UP_PRESSED) != 0)
player.moveUp();
if( (keyState & DOWN_PRESSED) != 0)
player.moveDown();
if( (keyState & RIGHT_PRESSED) != 0)
player.moveRight();
if( (keyState & LEFT_PRESSED) != 0)
player.moveLeft();
}
private void checkCollision() {
if(player.collidesWith(tiledCenary, false) == true) {
System.out.println("Collision!");
player.setLastPosition();
}
}
private void drawGameScreen(Graphics g) {
g.fillRect(0, 0, 256, 256);
player.paintSprite(g);
layer1.setViewWindow(player.getX() + 16, player.getY() + 16, 128, 110);
layer1.paint(g, 0, 0);
g.setColor(0, 0, 0);
flushGraphics();
}
}