Java2 ME:keyPressed() 在 GameCanvas 内不起作用

发布于 2024-12-21 00:40:53 字数 752 浏览 1 评论 0原文

我知道 J2ME 已经相当过时了,但我必须为了一项作业而这样做。目前,我正在使用 GameCanvas 类,我的游戏是一个线程,所以我的代码看起来像这样。

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...
    }
}

可悲的是,无论我的垃圾邮件多么严重,keyPressed 方法都不会被调用在模拟器的小键盘上。我知道 GameCanvas 具有的 getKeyStates() 方法,但我不想使用它,因为我不仅想捕获游戏按键,还想捕获数字键 1~9。

有谁知道为什么我的代码没有进入我的 keyPressed() 方法,以及我能做什么?非常感谢。


不知道我哪里出了问题......但是经过一些调整后,它开始工作得很好。非常感谢大家! :)

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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-12-28 00:40:53

您在 Game#run 方法中有一个忙等待,这很可能会导致设备会忽略您的所有点击,从而使您的 UI 响应能力松散。

为了简单测试上述假设是否正确,只需在循环中插入 sleep 即可,如下所示:

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

如果上述有助于恢复 UI 响应能力,请重新设计您的应用程序以避免繁忙等待 - MIDP API 提供了几种方法为了实现这一点。

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.

Hello爱情风 2024-12-28 00:40:53

MIDP 文档摘录 GameCanvas(...)

如果开发者只需使用 getKeyStates 方法查询按键状态, 当显示此 GameCanvas 时,可以抑制游戏按键的常规按键事件机制。如果应用程序不需要,抑制按键事件可以通过消除对 keyPressed、keyRepeated 和 keyReleased 方法的不必要的系统调用来提高性能。

请注意,按键事件可以仅对定义的游戏键(UP、DOWN、FIRE 等)进行抑制;始终为所有其他按键生成按键事件。

因此,super(false) 将抑制 GameCanvas 中的 Canvas 按键事件侦听器方法。在这种情况下,如果您仍然想注册按键事件,请在 run() 中使用 getKeyEvents(...)示例如下

 // 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();
 }

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();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文