我对如何实现LIBGDX控制器支持感到茫然。所以这里有详细信息。
第4部分教程的Git(我试图实现游戏手柄)在这里:https://github.com/obviam/star-assault/tree/part4
我已经完成了最后一步,现在它正在运行,我可以使用键盘与角色进行交互。我现在想通过控制器使用OUYA或USB来控制角色。
我已阅读以下内容:http://www.badlogicgames.com/wordpress/?p=2724 我已经将jar文件添加到主项目中并进行了Ordered和Exported。 我现在修改了我的GameScreen类,如下所示:
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.PovDirection;
import com.badlogic.gdx.controllers.mappings.Ouya;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Vector3;
public class GameScreen implements Screen, InputProcessor, ControllerListener {
private World world;
private WorldRenderer renderer;
private CharacterController charactercontroller;
private int width, height;
@Override
public void show() {
world = new World();
renderer = new WorldRenderer(world, false);
charactercontroller = new CharacterController(world);
Gdx.input.setInputProcessor(this);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
charactercontroller.update(delta);
renderer.render();
}
@Override
public void resize(int width, int height) {
renderer.setSize(width, height);
this.width = width;
this.height = height;
}
@Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
Gdx.input.setInputProcessor(null);
}
// * InputProcessor methods ***************************//
@Override
public boolean keyDown(int keycode) {
if (keycode == Keys.LEFT)
charactercontroller.leftPressed();
if (keycode == Keys.RIGHT)
charactercntroller.rightPressed();
if (keycode == Keys.UP)
charactercontroller.jumpPressed();
return true;
}
@Override
public boolean keyUp(int keycode) {
if (keycode == Keys.LEFT)
charactercontroller.leftReleased();
if (keycode == Keys.RIGHT)
charactercontroller.rightReleased();
if (keycode == Keys.UP)
charactercontroller.jumpReleased();
return true;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int x, int y, int pointer, int button) {
if (!Gdx.app.getType().equals(ApplicationType.Android))
return false;
if (x < width / 2 && y > height / 2) {
charactercontroller.leftPressed();
}
if (x > width / 2 && y > height / 2) {
charactercontroller.rightPressed();
}
return true;
}
@Override
public boolean touchUp(int x, int y, int pointer, int button) {
if (!Gdx.app.getType().equals(ApplicationType.Android))
return false;
if (x < width / 2 && y > height / 2) {
charactercontroller.leftReleased();
}
if (x > width / 2 && y > height / 2) {
charactercontroller.rightReleased();
}
return true;
}
@Override
public boolean touchDragged(int x, int y, int pointer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
@Override
public void connected(Controller controller) {
// TODO Auto-generated method stub
}
@Override
public void disconnected(Controller controller) {
// TODO Auto-generated method stub
}
@Override
public boolean buttonDown(Controller controller, int buttonCode) {
if (buttonCode == Ouya.BUTTON_O) {
charactercontroller.jumpPressed();
}
return false;
}
@Override
public boolean buttonUp(Controller controller, int buttonCode) {
if (buttonCode == Ouya.BUTTON_O) {
charactercontroller.jumpReleased();
}
return false;
}
@Override
public boolean axisMoved(Controller controller, int axisCode, float value) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean povMoved(Controller controller, int povCode,
PovDirection value) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean xSliderMoved(Controller controller, int sliderCode,
boolean value) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean ySliderMoved(Controller controller, int sliderCode,
boolean value) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean accelerometerMoved(Controller controller,
int accelerometerCode, Vector3 value) {
// TODO Auto-generated method stub
return false;
}
}
基于我对其工作原理的理解,现在当我在OUYA上加载它时,当按下“O”按钮时,角色应该跳转。但没有任何反应。我想我错过了什么。实施中有任何明显的错误?
我在Java工作的地方是多年来我一直在网络开发,最近又回到了Java,在微控制台上玩游戏和互动。我想从一个简单的教程开始,尝试构建它是个好主意,对吧?
期待获得一些非常棒的帮助!
谢谢,
菲尔
答案 0 :(得分:5)
尝试添加:
Controllers.addListener(this);
之前:
Gdx.input.setInputProcessor(this);
setInputProcessor
仅为内置输入事件(键,触摸事件等)注册侦听器。控制器是一个外部库,需要单独初始化和加载。
您也需要导入com.badlogic.gdx.controllers.Controllers
。