如何更新有关LibGDX控制器的旧代码?

时间:2018-08-05 13:28:46

标签: libgdx

我设计了一个开源游戏项目的蓝图,当我将此类复制并粘贴到我的项目中时  import com.badlogic.gdx.controllers.Controllers;不存在。我认为这段代码很旧。由于这种getControllers()方法不起作用。

如何更新此代码。我把这堂课放在下面。

package com.good.game.controls;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.controllers.Controllers; // this import does not exist

public class InputController {

    public static final InputController instance = new InputController();

    public InputController () {
        Gdx.input.setCatchBackKey(true);
    }

    public boolean controllerEnabled() {
        return (Controllers.getControllers().size > 0); //Cannot resolve Controllers
    }

    public boolean axisLeft() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getAxis(8) == -1 //Cannot resolve Controllers
                || Gdx.input.isKeyPressed(Keys.LEFT));
    }

    public boolean axisRight() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getAxis(8) == 1//Cannot resolve Controllers
                || Gdx.input.isKeyPressed(Keys.RIGHT));
    }

    public boolean axisUp() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getAxis(9) == -1)//Cannot resolve Controllers
                || Gdx.input.isKeyPressed(Keys.UP);
    }

    public boolean axisDown() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getAxis(9) == 1)//Cannot resolve Controllers
                || Gdx.input.isKeyPressed(Keys.DOWN);
    }

    public boolean buttonA() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getButton(96) || //Cannot resolve Controllers
                Gdx.input.isKeyPressed(Keys.Z));
    }

    public boolean buttonB() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getButton(97)||//Cannot resolve Controllers
                Gdx.input.isKeyPressed(Keys.C));
    }

    public boolean buttonX() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getButton(99) ||//Cannot resolve Controllers
                Gdx.input.isKeyPressed(Keys.X));
    }

    public boolean buttonY() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getButton(100));//Cannot resolve Controllers
    }

    public boolean rightTrigger() {
        return (controllerEnabled() && Controllers
                .getControllers().get(0).getAxis(7) == 1);//Cannot resolve Controllers
    }

    public boolean closeButton() {
        return Gdx.input.isKeyPressed(Keys.BACK) ||
                Gdx.input.isKeyJustPressed(Keys.ESCAPE);
    }

}

1 个答案:

答案 0 :(得分:0)

您必须将gdx-controllers扩展添加到您的依赖项中。它是从主项目中提取的。