当测试按下开始按钮时游戏无法开始

发布于 2024-11-29 01:36:18 字数 702 浏览 0 评论 0原文

我正在开发 2D 街机游戏并编写测试(在单独的 Android JUnit 测试项目中)。我需要运行游戏来进行一些测试方法。我编写了以下代码:

public void testGameStart() throws InterruptedException {
    _activity.runOnUiThread(
              new Runnable() {
                public void run() {
                    _gameView.requestFocus();
                } // end of run() method definition
              } // end of anonymous Runnable object instantiation
            ); // end of invocation of runOnUiThread
    //start game
    this.sendKeys(KeyEvent.KEYCODE_MENU,
            KeyEvent.KEYCODE_ENTER);
    Sub.sleep(10000); //wait 10 seconds

运行此测试时,我看到菜单出现并突出显示“开始”,但随后我没有看到游戏在模拟器上启动。但是,当我在测试运行期间单击“菜单”然后自己“启动”时,它就会启动。我做错了什么?

I'm developing a 2D arcade game and writing tests (in a seperate Android JUnit Test Project). I need game running for some test methods. I wrote the following code:

public void testGameStart() throws InterruptedException {
    _activity.runOnUiThread(
              new Runnable() {
                public void run() {
                    _gameView.requestFocus();
                } // end of run() method definition
              } // end of anonymous Runnable object instantiation
            ); // end of invocation of runOnUiThread
    //start game
    this.sendKeys(KeyEvent.KEYCODE_MENU,
            KeyEvent.KEYCODE_ENTER);
    Sub.sleep(10000); //wait 10 seconds

While running this test, I see menu appears and "start" highlighted, but then I don't see game starts on the emulator. But, when I click "MENU" and then "start" by myself during test run, it starts. What am I doing wrong?

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

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

发布评论

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

评论(2

追风人 2024-12-06 01:36:18

这对我来说很奇怪,但以下代码按照我需要的方式工作:

this.sendKeys(KeyEvent.KEYCODE_MENU);
this.sendKeys(KeyEvent.KEYCODE_ENTER);

感谢 AeroDroid 尝试回答。也许我之前的代码恰好将焦点放在按钮上。

This is strange to me, but the following code works the way I needed:

this.sendKeys(KeyEvent.KEYCODE_MENU);
this.sendKeys(KeyEvent.KEYCODE_ENTER);

Thanks AeroDroid for trying to answer. Maybe my previous code exactly gets focus on button.

阳光下慵懒的猫 2024-12-06 01:36:18

我不知道这是否直接回答了您的问题,但我将根据我认为您想说的内容来回答;如果我误解了,我深表歉意。我认为您所指的“突出显示”只是聚焦的开始按钮。如果您只是简单地关注开始按钮,那么当然不会按下或激活它。根据您提供的代码,我永远不会通过尝试强制用户执行操作来开始操作。如果你制作了菜单,你不知道如何开始游戏吗?您的程序中是否无法调用开始按钮中的内容?

I don't know if this directly answers your question, but I'm going to base my answer on what I think you're trying to say; I apologize if I misunderstand it. I think the "highlighting" you're referring to is just the start button being focused. The start button of course will not be pressed or activated if you are simple focused on it. From the code you've provided, I wouldn't ever start an action by trying to literally force what a user would do. If you made the menu, don't you know how to start the game. Is there no way in your program to call what's in the start button?

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