package MyGame;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
public class clsCanvas extends GameCanvas implements Runnable {
private boolean isRunning = true;
private Graphics g;
private midMain fParent;
public Sprite cursor;
public GameDesign gameDesign;
public clsCanvas(midMain m) {
super(true);
fParent = m;
}
public void start(){
Thread runner = new Thread(this);
runner.start();
}
public void run() {
this.gameDesign = new GameDesign();
try{
this.cursor = this.gameDesign.getCurs();
this.cursor.defineReferencePixel(cursor.getWidth(), cursor.getHeight());
}
catch(Exception x){}
int iKey = 0;
g = getGraphics();
g.setColor(200);
g.fillRect(0, 0, 50, 50);
cursor.paint(g);
while (isRunning) {
iKey = getKeyStates();
if ((iKey & GameCanvas.FIRE_PRESSED) != 0) {
isRunning = false;
}
if ((iKey & GameCanvas.RIGHT_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX()+5, cursor.getY());
cursor.paint(g);
}
if ((iKey & GameCanvas.LEFT_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX()-5, cursor.getY());
cursor.paint(g);
}
if ((iKey & GameCanvas.DOWN_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX(), cursor.getY()+5);
cursor.paint(g);
}
if ((iKey & GameCanvas.UP_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX(), cursor.getY()-5);
cursor.paint(g);
}
flushGraphics();
try {
Thread.sleep(10);
} catch (Exception ex) {
}
}
g = null;
fParent.destroyApp(false);
fParent = null;
}
}
Moving cursor I'm waiting ~ 250ms per 1 step. Where is a problem?
I tried with Thread.sleep(50), 30 and 10 - all the same:(
(Filling full screen not affect on speed)
Please, help.
EDIT:
I replace IFs with this and any difference.
while ((iKey & GameCanvas.RIGHT_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, width, height);
cursor.setPosition(cursor.getX()+5, cursor.getY());
cursor.paint(g);
flushGraphics();
try{
Thread.sleep(10);}
catch(Exception x){}
iKey = getKeyStates();
}
And if I quickly pressing RIGHT, cursor moving faster than I press and hold RIGHT key.