等待触摸输入而不阻止游戏线程

rod*_*dit 5 java android game-engine

我最近一直在为Android开发游戏.该游戏的格式与经典的口袋妖怪游戏相似(非常复杂).我有很多没有任何问题.但是,我发现当有人说话时,很难开发出现在屏幕底部的对话(打字机文本)框.我坚持等待任何输入而不阻止渲染和更新方法(因为它们都在同一个线程上运行).

这是我的游戏循环的简化版本,概述了结构以及我每帧的作用.显示的更新方法通过View#onDraw方法调用.

public static void update(Canvas canvas, GameView view){
    //time operations (calculating delta etc.)
    long cTime = System.currentTimeMillis();
    delta = cTime - time;
    time = cTime;
    globals.put("time", time);
    globals.put("delta", delta);//set vars for scripts

    camera.applyToCanvas(canvas);//apply camera matrix to canvas
    if(map != null)map.update();
    if(battle != null)map.update();
    Effect.updateAll();//update everything (game logic)

    render.load(canvas);//load the canvas into my custom render engine
    render();//call render method (shown below)
    view.invalidate();//reset view for redraw
}

public static void render(){
    render.reset();//clears the canvas using canvas.setColor(Color.BLACK)
    if(map != null)map.draw();
    if(battle != null)battle.draw();
    if(dialog != null)dialog.draw();
    if(gui != null)gui.draw(); //draw everything using the renderengine
}
Run Code Online (Sandbox Code Playgroud)

如您所见,这些操作会更新所有游戏组件(地图,战斗,guis等),然后将它们渲染到屏幕上.通过查看代码,很明显我在同一个线程上运行这些操作,因此这个线程显然不会被阻塞(否则游戏就会停止).

这就是我的问题所在.我在游戏gui中使用了几个按钮.这些按钮被分配为8个整数(0-8),例如,当按下向上按钮时,该输入数据通过游戏传递到现有对话框或战斗或任何需要它.这些按钮依赖于主要活动的触摸事件,其中MotionEvent由于运行游戏循环离开onDraw方法而在与游戏本身相同的线程上进行调整.以下是我处理对话框的方法:

  1. 我使用javascript(rhino解析器)如下(将对话框对象传递给变量'dialog'中的脚本):var d = dialog.show("Hello!How are you.");

  2. 为了获得输入,我调用getInput方法接受对话框,最多4个字符串作为选择选项.该方法应该返回一个整数值.这可以通过脚本调用如下: var response = dialog.getInput(d, "I'm fine, thank you!", "Who wants to know.");

  3. getConput方法在我的Dialog类中如下:

    public static int getInput(Dialog d, String... args){
        d.waitingForInput = true;
        d.lastInput = -1;
        while(d.waitingForInput || d.lastInput == -1){
            //render options
            //main thread is obviously blocked here.
        }
        return d.getLastInput();//returns the d.lastInput and resets waitingForInput and lastInput. The script can then use this value to decide what to do.
    }
    
    Run Code Online (Sandbox Code Playgroud)

现在我的主要问题是如何在不阻塞渲染和更新线程的情况下运行此方法.更具体地说,当用户触摸屏幕的某个区域(gui选择按钮所在的位置)时,设置d.lastInput和d.waitingForInput的值.

如果对我在这个问题中要问的内容有任何疑问,请发表评论,因为我很累,并且花了一些时间试图解决这个问题.

编辑:

我曾想过在单独的线程上运行输入,但这仍然会导致阻止其他输入方法被调用(例如更改对话框中的选定选项).

rod*_*dit 0

我决定通过某种形式的事件处理程序来解决这个问题。当所需的输入下降时,游戏将运行指定的脚本。