我正在使用libgdx框架来创建游戏。我试图创建屏幕按钮/控件。
目前,我有一个
class LevelOne that implements Screen.
这个类有一个私有变量 world(来自Box2d)。
我想在Box2d世界中添加一个带有文本按钮的表格或Libgdx触摸板。但是,我不确定如何做到这一点。
接下来,我知道可以将表格或触摸板添加到Libgdx舞台上。是否有任何方式让Libgdx舞台和Box2d世界一起工作,以便我可以将触摸板或表格添加到Box2d世界中?
我正在使用libgdx框架来创建游戏。我试图创建屏幕按钮/控件。
目前,我有一个
class LevelOne that implements Screen.
对于屏幕上的控件,您可以按照以下方式进行:
创建一个新的摄像机,用于固定控件:
OrthographicCamera guicam = new OrthographicCamera(480, 320);
guicam.position.set(480/2F, 320/2F, 0);
Rectangle wleftBounds = new Rectangle(0, 0, 80, 80);
Rectangle wrightBounds = new Rectangle(80, 0, 80, 80);
Vector3 touchPoint = new Vector3();
接下来,您可以轮询输入以查看用户是否触摸了这些矩形:
//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!
}
}
请注意,我正在检查前5个触摸索引,因为您肯定希望同时使用的控件(例如向右移动时跳跃)。
最后但并非最不重要的是,您需要在控件上绘制一些漂亮的图形:
batch.draw(leftRegion, wleftBounds.x, wleftBounds.y, wleftBounds.width, wleftBounds.height);
batch.draw(rightRegion, wrightBounds.x, wrightBounds.y, wrightBounds.width, wrightBounds.height);
HUDMatrix = camera.combined.cpy();
HUDMatrix.setToOrtho2D(0, 0, wwidth, wheight);
然后绘制它的HUD
batch.setProjectionMatrix(HUDMatrix);
batch.begin();
stage.draw(batch)
batch.end();