Android 中处理用户输入事件的基础知识是什么?

发布于 2024-08-28 19:38:10 字数 1316 浏览 9 评论 0原文

我以为我已经理解了这个问题,但这里有些不对劲。当用户(到目前为止,我)尝试按键时,什么也没有发生,而且我很难理解我错过了什么。

在我提供一些代码来帮助澄清我的问题之前,请考虑一下这一点:我正在使用 Android 的 Lunar Lander 示例来制作我的第一个“真正的”Android 程序。当然,在该示例中,存在一个 LunarView 类,以及嵌套在其中的 LunarThread 类。在我的代码中,这些类的等效项分别是 Graphics 和 GraphicsThread。

我还可以在 Android 上制作 2D 精灵动画。我有一个 Player 类,假设 GraphicsThread 有一个称为“player”的 Player 成员。该类有四个坐标 - x1、y1、x2 和 y2 - 它们定义了一个要在其中绘制精灵的矩形。我已经解决了,所以我可以完美地处理这个问题。每当调用 doDraw(Canvas canvas) 方法时,它只会查看这些坐标的值并相应地绘制精灵。

现在让我们说 - 这并不是我真正想要对程序执行的操作 - 我正在尝试使程序所做的只是在屏幕的一个位置显示玩家精灵,直到用户第一次使用按下 Dpad 的左按钮。然后该位置将更改为屏幕上的另一个设定位置,并且对于程序的其余部分,精灵将始终绘制在该位置。

另请注意,Graphics中的GraphicsThread成员称为“thread”,GraphicsThread中的SurfaceHolder成员称为“mSurfaceHolder”。

因此,请在 Graphics 类中考虑此方法:

@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
    return thread.keyDownHandler(keyCode, msg);
}

另外,请在 GraphicsThread 类中考虑此方法:

boolean keyDownHandler(int keyCode, KeyEvent msg) {
    synchronized (mSurfaceHolder) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            player.x1 = 100;
            player.y1 = 100;
            player.x2 = 120;
            player.y2 = 150;
        }
    }
    return true;
}

现在假设玩家的坐标从 (200, 200, 220, 250) 开始,为什么当我按下 Dpad: Left 时他不会做任何不同的事情?

谢谢!

I thought I had understood this question, but something is quite wrong here. When the user (me, so far) tries to press keys, nothing really happens, and I am having a lot of trouble understanding what it is that I've missed.

Consider this before I present some code to help clarify my problem: I am using Android's Lunar Lander example to make my first "real" Android program. In that example, of course, there exist a class LunarView, and class nested therein LunarThread. In my code the equivalents of these classes are Graphics and GraphicsThread, respectively.

Also I can make sprite animations in 2D just fine on Android. I have a Player class, and let's say GraphicsThread has a Player member referred to as "player". This class has four coordinates - x1, y1, x2, and y2 - and they define a rectangle in which the sprite is to be drawn. I've worked it out so that I can handle that perfectly. Whenever the doDraw(Canvas canvas) method is invoked, it'll just look at the values of those coordinates and draw the sprite accordingly.

Now let's say - and this isn't really what I'm trying to do with the program - I'm trying to make the program where all it does is display the Player sprite at one location of the screen UNTIL the FIRST time the user presses the Dpad's left button. Then the location will be changed to another set position on the screen, and the sprite will be drawn at that position for the rest of the program invariably.

Also note that the GraphicsThread member in Graphics is called "thread", and that the SurfaceHolder member in GraphicsThread is called "mSurfaceHolder".

So consider this method in class Graphics:

@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
    return thread.keyDownHandler(keyCode, msg);
}

Also please consider this method in class GraphicsThread:

boolean keyDownHandler(int keyCode, KeyEvent msg) {
    synchronized (mSurfaceHolder) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            player.x1 = 100;
            player.y1 = 100;
            player.x2 = 120;
            player.y2 = 150;
        }
    }
    return true;
}

Now then assuming that player's coordinates start off as (200, 200, 220, 250), why won't he do anything different when I press Dpad: Left?

Thanks!

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

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

发布评论

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

评论(2

独留℉清风醉 2024-09-04 19:38:10

在我担心实际运动之类的事情之前,我会考虑记录...

类似这样的事情:

Log.d("lunar", "keyCode = ["+String.valueOf(keyCode)+"] // msg = ["+String.valueOf(msg)+"]");

这样做时,我可以先了解系统正在注册什么,然后再担心我如何处理所述注册数据...之后您可以决定是否向其发送正确的内容,然后可以担心线程工作等。

希望这可以帮助诊断等。(所有这些都是徒手编写的,可能包含错误)

Before I would worry about actual movement and the like I would consider Log...

Something like:

Log.d("lunar", "keyCode = ["+String.valueOf(keyCode)+"] // msg = ["+String.valueOf(msg)+"]");

In doing so I can get a feel for what the system is registering before I worry about what I do with said registered data... After that you can decide if you're even sending it the right stuff and can then worry about thread work etc.

Hopefully that can help diagnose etc.(All of this was written freehand, may contain errors)

日记撕了你也走了 2024-09-04 19:38:10

扔掉 LunarLander 并使用真正的指南:玩Android 中的图形

Throw away LunarLander and use a real guide: Playing with graphics in Android

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