我不知道如何實現LIBGDX控制器支持。所以這裏是細節。在LIBGDX中實現遊戲手柄控制器?
Git的在第4部分(這裏我試圖實現一個手柄)的教程是在這裏: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;
}
}
因此,基於我的理解是如何工作的,當「O」按鈕,當我打開這對歐雅字符應該跳轉現在按下。但沒有任何反應。我想我錯過了一些東西。執行中有任何明顯的錯誤?
我在哪裏與Java在一起,我一直在網絡開發多年,最近剛剛回到Java來玩遊戲和微型控制檯上的交互式東西。我從一個簡單的教程開始,嘗試構建它會是一個好主意,對吧?
期待獲得一些很棒的幫助!
感謝,
菲爾
在連接/斷開連接的方法和buttonDown方法中添加一些日誌記錄,以查看它們是否被調用。 (我認爲buttonCodes在某些平臺上可能有點不可思議。) –
會做!我嘗試將我的OUYA控制器與我的Andoroid平板電腦配對,並通過日誌記錄進行測試,但是無論何時按下按鈕,它都會完全離開應用程序。幾個小時後回家時,我將不得不在OUYA上進行測試。 –
@ P.T。我在連接的方法和buttonup/buttondown方法中都添加了一個System.out.println,沒有任何反應。任何其他想法?我是否需要在我的gameScreen類中創建一個控制器偵聽器或任何東西? –