在 LIBGDX 中实现游戏手柄控制器



我不知道如何实现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

我已经完成了最后一步,现在它正在运行,我可以使用键盘与角色互动。我现在想通过带有 OOYA 或 USB 的控制器来控制角色。

我已阅读以下内容:http://www.badlogicgames.com/wordpress/?p=2724 我已将 jar 文件添加到主项目中,并已排序和导出。 我现在修改了我的游戏屏幕类,如下所示:

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;
}
}

因此,根据我对它如何工作的理解,现在当我在 OOYA 上加载它时,当按下"O"按钮时,角色应该会跳跃。但什么也没发生。我想我错过了一些东西。在实施中有什么明显的错误吗?

我在Java方面所处的位置是,我已经在Web开发了很多年,最近又回到了Java,在微型控制台上玩游戏和交互式东西。我认为从一个简单的教程开始并尝试在其上构建将是一个好主意,对吧?

期待得到一些很棒的帮助!

谢谢

菲尔

尝试添加:

Controllers.addListener(this);

就在之前:

Gdx.input.setInputProcessor(this);

setInputProcessor 仅为内置输入事件(按键、触摸事件等(注册侦听器。 控制器是一个外部库,需要单独初始化和加载。

您还需要导入com.badlogic.gdx.controllers.Controllers ,。

相关内容

  • 没有找到相关文章

最新更新