如何在libgdx中向ApplicationAdapter添加按钮?

时间:2017-05-03 09:47:24

标签: java android button 3d libgdx

我正在使用libgdx。

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

我的目标是在此视图中添加一个按钮,以便我可以转到其他视图。

目前我有相机,相机控制器,环境和两种型号。

例如: enter image description here

到目前为止我写的代码:

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

}

1 个答案:

答案 0 :(得分:1)

作为一名评论者提到,Scene2D是LibGDX的库。

我正在滚动我自己的按钮系统并且想出了如何做得好,这里是链接:https://stackoverflow.com/a/43792814/1159741

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

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

if(Gdx.input.justTouched()) handleInput();

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