[英]Is there a clean way to make a libgdx cross-platform controls implementation
I am currently just started working with Libgdx. 我目前刚开始与Libgdx合作。 I reached a point where I would like to consider cross-platform (PC/handheld) issues. 我到了要考虑跨平台(PC /手持式)问题的地步。 One important issue I can't seem to find a way around it is the following: I don't want each controllable entity to be an extension of InputAdapter, and then contain if(PC){switch: case key: increaseSpeed(); 我似乎找不到解决的一个重要问题,它是:我不希望每个可控实体都是InputAdapter的扩展,然后包含if(PC){switch:case key:gainSpeed();。 } else if(handheld) {switch: case touch: increaseSpeed();} } else if(掌上电脑){switch:case touch:gainSpeed();}
Is there a to implement something so that the entities would just receive the Program-related meanings of user-inputs? 是否有实现某些东西的方法,以便实体仅接收与程序相关的用户输入含义?
Sorry if my question is too vague :' ( 抱歉,我的问题太含糊:'(
You should have a look at this: Interfacing with plaform specific code . 您应该看一下: 与平台特定代码的接口 。
But in your case you would probably not use an interface
. 但是在您的情况下,您可能不会使用interface
。 The basic idea is that in your different platform specific projects, you would implement an InputAdapter
, for example DesktopController extends InputAdapter
. 基本思想是,在不同平台特定的项目中,您将实现一个InputAdapter
,例如DesktopController extends InputAdapter
。 Then you would supply this InputAdapter
to your ApplicationListener
. 然后,您可以将此InputAdapter
提供给ApplicationListener
。 Somehow like this: 像这样:
public class MyGame implements ApplicationListener {
private final InputAdapter controller;
public MyGame(InputAdapter controller) {
this.controller = controller;
// this controller can now be used
// Gdx.input.setInputProcessor(controller);
}
}
public static void main(String[] argv) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
new LwjglApplication(new MyGame(new DesktopController()), config);
}
Generally Libgdx apps just handle all the inputs, and don't worry about which device they're on. 通常,Libgdx应用程序只处理所有输入,而不用担心它们在哪个设备上。 Like this: 像这样:
...
@Override
public boolean keyDown(int keycode) {
if (keycode == Keys.RIGHT)
increaseSpeed();
return true;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
increaseSpeed();
return true;
}
This is using the event-based InputProcessor
to handle input events. 这是使用基于事件的InputProcessor
来处理输入事件。
LibGDX does map desktop mouse events onto "touch" events so you can collapse that part of PC/Android input handling, but there is nothing generic that maps keyboard keys to touch events or anything like that. LibGDX确实将桌面鼠标事件映射到“触摸”事件,因此您可以折叠PC / Android输入处理的该部分,但是没有通用的映射键盘键来映射触摸事件或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.