我正在使用 libgdx 框架创建游戏。我正在尝试创建屏幕按钮/控件。
目前,我有一个
class LevelOne that implements Screen.
这个类有一个私有(private)变量 world(来自 Box2d)
我要添加一个 带文本按钮的表格或 Box2d 世界的 Libgdx 触摸板。 但是,我不确定该怎么做。
接下来,我知道我可以将表格或触摸板添加到 Libgdx Stage。无论如何让 Libgdx 舞台和 Box2d 世界一起工作,所以我可以向 Box2d 世界添加触摸板或表格。
最佳答案
对于屏幕控制,你可以这样做:
制作一个新的凸轮,用于固定控件:
OrthographicCamera guicam = new OrthographicCamera(480, 320);
guicam.position.set(480/2F, 320/2F, 0);
为每个控件制作一个 (libgdx) 矩形:
Rectangle wleftBounds = new Rectangle(0, 0, 80, 80);
Rectangle wrightBounds = new Rectangle(80, 0, 80, 80);
创建一个新的 Vector3 来保存未投影的触摸坐标:
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);
关于android - Libgdx 创建屏幕控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917934/