Question

I know J2ME is pretty outdated, but I have to do this for an assignment. Currently, I am using the GameCanvas class, and my game is a thread, so my code looks something like this..

class Game extends GameCanvas implements Runnable {
    public GameCanvas() {
        super(false);
    }

    public void run() {
        while (true) {
            draw();
            flushGraphics();
        }
    }

    protected void keyPressed(int keyCode) {
        System.out.println("Hey, it actually worked.");
        // other code to handle key press...
    }
}

The sad thing is that the keyPressed method never gets called no matter how hard I spam hits on the emulator's numpad. I know of the getKeyStates() method that GameCanvas has, but I don't want to use it because I want to capture not just the game keys, but also the number keys 1~9.

Does anyone have any idea why my code doesn't go into my keyPressed() method, and what I can do about it? Many thanks.


Don't know where I went wrong... but after tweaking a little here and there, it started working perfectly fine. Thanks a lot guys! :)

No correct solution

OTHER TIPS

You have a busy wait within Game#run method which most likely causes device ignore all your hits, making your UI loose responsiveness.

For simple test if above assumption is correct, just insert sleep within the loop, about like below:

    while (true) {
        draw();
        flushGraphics();
        try { Thread.sleep(100); } // sleep for 1/10 sec
        catch (InterruptedException ie) { System.out.println(ie); }
    }

If above helps to recover UI responsiveness, redesign your application to avoid busy waits - MIDP API provides a couple of ways to achieve that.

The MIDP documentation excerpt for GameCanvas(...)

If the developer only needs to query key status using the getKeyStates method, the regular key event mechanism can be suppressed for game keys while this GameCanvas is shown. If not needed by the application, the suppression of key events may improve performance by eliminating unnecessary system calls to keyPressed, keyRepeated and keyReleased methods.

Note that key events can be suppressed only for the defined game keys (UP, DOWN, FIRE, etc.); key events are always generated for all other keys.

So super(false) will suppress the Canvas key event listener methods in GameCanvas. In which case if you still want to register the key events use getKeyEvents(...) in your run(), the example is as under

 // Get the Graphics object for the off-screen buffer
 Graphics g = getGraphics();

 while (true) {
    // Check user input and update positions if necessary
    int keyState = getKeyStates();
    if ((keyState & LEFT_PRESSED) != 0) {
          sprite.move(-1, 0);
    }
    else if ((keyState & RIGHT_PRESSED) != 0) {
          sprite.move(1, 0);
    }

    // Clear the background to white
    g.setColor(0xFFFFFF);
    g.fillRect(0,0,getWidth(), getHeight());

    // Draw the Sprite
    sprite.paint(g);

    // Flush the off-screen buffer
    flushGraphics();
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top