Libgdx在屏幕控制上创建

Man*_*eep 4 android box2d libgdx

我正在使用libgdx框架来创建游戏.我正在尝试创建屏幕按钮/控件.

目前,我有一个

class LevelOne that implements Screen. 
Run Code Online (Sandbox Code Playgroud)

这个类有一个私有变量世界(来自Box2d)

我想在Box2d世界中添加一个带有Textbuttons或Libgdx触控板的表格.但是,我不知道该怎么做.

接下来,我知道我可以在Libgdx Stage中添加表格或触摸板.无论如何,让Libgdx阶段和Box2d世界一起工作,我可以在Box2d世界中添加触摸板或表格.

Les*_*tat 9

对于屏幕控制,您可以这样做:

制作一个新的凸轮,将为控件固定:

OrthographicCamera guicam = new OrthographicCamera(480, 320);
guicam.position.set(480/2F, 320/2F, 0);
Run Code Online (Sandbox Code Playgroud)

为每个控件创建一个(libgdx)Rectangle:

Rectangle wleftBounds = new Rectangle(0, 0, 80, 80);
Rectangle wrightBounds = new Rectangle(80, 0, 80, 80);
Run Code Online (Sandbox Code Playgroud)

创建一个新的Vector3来保存未投影的触摸坐标:

Vector3 touchPoint = new Vector3();
Run Code Online (Sandbox Code Playgroud)

然后,您可以轮询输入以查看用户是否正在触摸这些矩形:

//in render method
for (int i=0; i<5; i++){
    if (!Gdx.input.isTouched(i)) continue;
    guicam.unproject(touchPoint.set(Gdx.input.getX(i), Gdx.input.getY(i), 0));
    if (wleftBounds.contains(touchPoint.x, touchPoint.y)){
        //Move your player to the left!
    }else if (wrightBounds.contains(touchPoint.x, touchPoint.y)){
        //Move your player to the right!
    }
}
Run Code Online (Sandbox Code Playgroud)

注意我正在检查前5个触摸索引,那是因为你肯定希望同时使用控件(即在向右移动时跳转).

最后但同样重要的是,您需要在控件上绘制一些漂亮的图形:

batch.draw(leftRegion, wleftBounds.x, wleftBounds.y, wleftBounds.width, wleftBounds.height);
batch.draw(rightRegion, wrightBounds.x, wrightBounds.y, wrightBounds.width, wrightBounds.height);
Run Code Online (Sandbox Code Playgroud)