我不知道如何实现 LIBGDX 控制器支持。所以这里是细节。
假设我在这里学习教程:http: //obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a- day-tutorial-part-1/
第 4 部分教程的 Git(我正在尝试实现游戏手柄)在这里:https ://github.com/obviam/star-assault/tree/part4
我已经完成了最后一步,现在它正在运行,我可以使用键盘与角色进行交互。我现在想通过使用 OUYA 或 USB 的控制器来控制角色。
我已阅读以下内容:http ://www.badlogicgames.com/wordpress/?p=2724 我已将 jar 文件添加到主项目并已订购和导出。我现在修改了我的 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 方面的工作是我已经从事 Web 开发多年,最近才再次回到 Java,在微型控制台上玩游戏和交互的东西。我认为从一个简单的教程开始并尝试在它的基础上进行构建将是一个好主意,对吧?
期待得到一些很棒的帮助!
谢谢,
菲尔