当测试按下开始按钮时游戏无法开始
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我来说很奇怪,但以下代码按照我需要的方式工作:
感谢 AeroDroid 尝试回答。也许我之前的代码恰好将焦点放在按钮上。
This is strange to me, but the following code works the way I needed:
Thanks AeroDroid for trying to answer. Maybe my previous code exactly gets focus on button.
我不知道这是否直接回答了您的问题,但我将根据我认为您想说的内容来回答;如果我误解了,我深表歉意。我认为您所指的“突出显示”只是聚焦的开始按钮。如果您只是简单地关注开始按钮,那么当然不会按下或激活它。根据您提供的代码,我永远不会通过尝试强制用户执行操作来开始操作。如果你制作了菜单,你不知道如何开始游戏吗?您的程序中是否无法调用开始按钮中的内容?
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?