Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 15-Sep-10 0:50am
v4

Is you code relying on an exception?

Why would Thread.Sleep ever throw an exception?

I suggest you do some analysis on each section of your code to findout how long it is in each section.

Sleep(10) in a loop should be enough.
 
Share this answer
 
Comments
[no name] 15-Sep-10 6:49am    
Works without any exceptions, and without "try{" I can't build the code.
I would refactor the code:

JavaScript
int height = getHeight();
int width  = getWidth();
while (isRunning) 
{
    iKey = getKeyStates();
    isRunning = ((iKey & GameCanvas.FIRE_PRESSED) != 0);
    if (isRunning)
    {
        int cursorX = cursor.GetX();
        int cursorY = cursor.GetY();
        bool refresh = false;
        if ((iKey & GameCanvas.RIGHT_PRESSED) != 0) 
        {
            refresh = true;
            cursorX += 5;
        }
        if ((iKey & GameCanvas.LEFT_PRESSED) != 0) 
        {
            refresh = true;
            cursorX -= 5;
        }
        if ((iKey & GameCanvas.DOWN_PRESSED) != 0) 
        {
            refresh = true;
            cursorY += 5;
        }
        if ((iKey & GameCanvas.UP_PRESSED) != 0) 
        {
            refresh = true;
            cursorY -= 5;
        }
	if (refresh)
        {
            g.setColor(200);
            g.fillRect(0, 0, width, height);
            cursor.setPosition(cursorX, cursorY);
            cursor.paint(g);
            flushGraphics();            
        }
    } 
    Thread.sleep(10);
}
 
Share this answer
 
v2
Comments
[no name] 15-Sep-10 8:49am    
Thanks, however I still spent 11 seconds to move via my 'Siemens M75' display width.
And if it impossible for some reason, as 1 solution I will have to use keyboard to change size of cursor's step, to get desired accuracy and partially speed.. Sad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900