Libgdx创建屏幕控制 [英] Libgdx Creating On Screen Controls

查看:127
本文介绍了Libgdx创建屏幕控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用libgdx框架来创建一个游戏。我试图创建屏幕上的按钮/控制。

I am using the libgdx framework to create a game. I'm trying create onscreen buttons/controls.

目前,我有一个

class LevelOne that implements Screen. 

本类有一个私有变量世界(从Box2D的)

This class has a private variable world (From Box2d)

我要添加
    表Textbuttons或Libgdx触摸板的Box2D的世界。
但是,我不知道如何做到这一点。

I want to add a Table with Textbuttons or a Libgdx Touchpad to the Box2d world. However, I'm not sure how to do this.

接下来,我知道我可以表或触摸板添加到Libgdx舞台。反正是有得到Libgdx阶段的Box2D世界这么一起工作,我可以在触摸板或表添加到box2d的世界。

Next, I know I can add a table or a touchpad to a Libgdx Stage. Is there anyway to get the Libgdx stage and Box2d world to work together so, I can add a Touchpad or Table to the Box2d world.

推荐答案

有关屏幕控制,你可以做这样的:

For onscreen controls, you can do it like this:

请新的凸轮将被固定的控件:

Make a new cam which will be fixed for the controls:

OrthographicCamera guicam = new OrthographicCamera(480, 320);
guicam.position.set(480/2F, 320/2F, 0);

请一(libgdx)矩形为每个控件:

Make a (libgdx) Rectangle for each control:

Rectangle wleftBounds = new Rectangle(0, 0, 80, 80);
Rectangle wrightBounds = new Rectangle(80, 0, 80, 80);

然后就可以查询输入,看看用户是否接触这些矩形:

Then you can poll the Input to see if the user is touching these rectangles:

//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个触摸索引,那是因为你一定会希望有正在同时使用的控制(即跳边移动边右)。

Notice I'm checking the first 5 touch indexes, thats because you will surely want to have controls that are being used at the same time (i.e. Jumping while Moving Right).

最后但并非最不重要的,你会希望通过控制来画一些漂亮的图形:

Last but not least, you will want to draw some nice graphics over the controls:

batch.draw(leftRegion, wleftBounds.x, wleftBounds.y, wleftBounds.width, wleftBounds.height);
batch.draw(rightRegion, wrightBounds.x, wrightBounds.y, wrightBounds.width, wrightBounds.height);

这篇关于Libgdx创建屏幕控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆