Java 如何在libgdx中将按钮添加到ApplicationAdapter?

Java 如何在libgdx中将按钮添加到ApplicationAdapter?,java,android,button,3d,libgdx,Java,Android,Button,3d,Libgdx,我正在使用libgdx 到目前为止,我有两个圆柱体,能够旋转它们 我的目标是在这个视图中添加一个按钮,这样我就可以转到其他视图 目前我有照相机、cameracontroller、环境和两种型号 例如: 到目前为止我编写的代码: public class FirstOfTheAndroid extends ApplicationAdapter { @Override public void create() { // adding a new Renderer modelBa

我正在使用libgdx

到目前为止,我有两个圆柱体,能够旋转它们

我的目标是在这个视图中添加一个按钮,这样我就可以转到其他视图

目前我有照相机、cameracontroller、环境和两种型号

例如:

到目前为止我编写的代码:

public class FirstOfTheAndroid extends ApplicationAdapter {
@Override
public void create() {

    // adding a new Renderer
    modelBatch = new ModelBatch();

    // adding a new environment
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

    // adding a camera
    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(10f, 10f, 10f);
    cam.lookAt(0, 0, 0);
    cam.near = 1f;
    cam.far = 300f;
    cam.update();

    // adding a new model
    ModelBuilder modelBuilder = new ModelBuilder();
    model = modelBuilder.createCylinder(4f, 6f, 4f, 16,
            new Material(
                    ColorAttribute.createDiffuse(Color.GOLD),
                    ColorAttribute.createSpecular(1, 1, 1, 1),
                    FloatAttribute.createShininess(8f)),
            VertexAttributes.Usage.Position
                    | VertexAttributes.Usage.Normal
                    | VertexAttributes.Usage.TextureCoordinates);
    model2 = modelBuilder.createCylinder(5f, 5f, 5f, 16,
            new Material(
                    ColorAttribute.createDiffuse(Color.GOLD),
                    ColorAttribute.createSpecular(1, 1, 1, 1),
                    FloatAttribute.createShininess(8f)),
            VertexAttributes.Usage.Position
                    | VertexAttributes.Usage.Normal
                    | VertexAttributes.Usage.TextureCoordinates);

    instance = new ModelInstance(model);
    instance2 = new ModelInstance(model2);

    // adding a camera control
    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);
}

@Override
public void render() {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(84/255f, 84/255f, 84/255f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    modelBatch.begin(cam);
    modelBatch.render(instance, environment);
    modelBatch.render(instance2, environment);
    modelBatch.end();

    camController.update();
}

}

正如一位评论者所提到的,Scene2D是LibGDX的库

我正在滚动我自己的按钮系统,刚刚找到了如何做好它的方法,下面是链接:

最后,这里有一个简单的方法-暴力,只有当你在整个游戏中只有几个按钮时才有效

在游戏类中创建
handleInput()
方法。在render()中:

handleInput()

Vector3 touchCoords=newvector3(Gdx.input.getX(),Gdx.input.getY(),0);//如果从摄影机切换到视口,请使用矢量2
照相机。取消投影(触摸坐标);//将屏幕坐标转换为游戏坐标
如果(touchCoords.x>[按钮左侧x]&&
touchCoords.x<[按钮右侧x]&&
touchCoords.y>[按钮底部y]&&
touchCoords.y<[按钮顶部y])
//在这里做点什么
else if(…检查其他按钮的坐标)
//那就这样做吧
//以此类推,以获取更多按钮

阅读场景2D上的文档。
if(Gdx.input.justTouched()) handleInput();
    Vector3 touchCoords = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); // use a Vector2 if you switch from Camera to Viewport
    camera.unproject(touchCoords); // converts screen coordinates to game coordinates
    if(touchCoords.x > [button left side x] &&
       touchCoords.x < [button right side x] &&
       touchCoords.y > [button bottom y] &&
       touchCoords.y < [button top y])
        // do something here
    else if(... check coordinates for the other button)
         // then do this
    // and so on for additional buttons