gpt4 book ai didi

java - 为 Libgdx 制作随相机移动的 UI

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:42 24 4
gpt4 key购买 nike

我有一个 box2d 屏幕,您可以在其中移动相机。我希望能够在屏幕周围显示随相机移动的 scene2d 菜单。现在的问题是我不知道该怎么做。下面的 BuildScreen 扩展自 AbstractControllerScreen,它是一个实现 Screen 并使用 ViewScreen 和 ModelScreen 的类。这些与 MVC 模型相匹配。下面是代表我想要菜单和 box2d 区域的屏幕。如果代码太多,我事先表示歉意,但这是我必须处理的。

公共(public)类 BuildScreen 扩展 AbstractControllerScreen{

private World world;
private List<BuildPart> ships;


private float r = 70f*BodyConstants.WORLD_TO_BOX;

private Renderable stageRendrable;
private final Stage stage;
private Entity stageEntity;
private InputMultiplexer inputHandler;


public BuildScreen (Nimby game) {
super(game, new CameraControllerBuild());
ships = new LinkedList<>();
World world = new World(Vector2.Zero, true);
this.world = world;
inputHandler = new InputMultiplexer();


stageRendrable = new Renderable() {

@Override
public void render(final SpriteBatch batch) {
Table.drawDebug(stage);

}

@Override public void debug(final ShapeRenderer sr) { }
};

stageEntity = new Entity() {
public void update(final float delta, final OrthographicCamera cam) {
float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
stage.act(delta);

}
};

stage = new Stage();
stage.setCamera(getViewScreen().getCam());
inputHandler.addProcessor(stage);


Bundle stageBundle = new Bundle(stageEntity, stageRendrable);

addBundles(stageBundle);

//Box2d代码省略//

    Bundle b = new Bundle(engine, engine);
addBundles(b);

inputHandler.addProcessor(new BuildScreenInputProcessor(world, getCamera(), stage));


world.setContactListener(new NodeSnapper());
Gdx.input.setInputProcessor(inputHandler);
}

public void resize(int width, int height) {
getViewScreen().getCam().setToOrtho(false, 1, height / width);
if (stage != null) {
stage.clear();
}

TextureAtlas atlas = new TextureAtlas("ui/morebuttons.pack");

Skin skin = new Skin(atlas);
Table table = new Table(skin);

table.setBounds(0, 0, width, height);
BitmapFont white = new BitmapFont(Gdx.files.internal("fonts/terminal.fnt"), false);

/* Making a button style */
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("blankbutton");
textButtonStyle.down = skin.getDrawable("blankbuttonpressed");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = white;
textButtonStyle.fontColor = Color.BLACK;

/*Creates button*/
TextButton newButton = new TextButton("NEW", textButtonStyle);

table.add(newButton);


stage.addActor(table);

getViewScreen().setStage(stage);
}

ViewScreen 类,如果有兴趣的话,看起来像这样。

公共(public)类ViewScreen{

private OrthographicCamera cam;
private ShapeRenderer sr;
private SpriteBatch batch;
private List<Renderable> renderables;
private CameraController camControll;
private Stage stage;
private Box2DDebugRenderer d;
/**
* @return the cam
*/
public OrthographicCamera getCam() {
return cam;
}

/**
* @return the camControll
*/
public CameraController getCamControll() {
return camControll;
}

public ViewScreen(final OrthographicCamera ocam, final CameraController cameraController) {
this.cam = ocam;
d = new Box2DDebugRenderer();
renderables = new LinkedList<Renderable>();
batch = new SpriteBatch();
sr = new ShapeRenderer();

setCamControll(cameraController);
}

public synchronized void render(float delta) {
getCamControll().update();

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

// RENDER SPRITES

batch.setProjectionMatrix(cam.combined);
batch.begin();

Iterator<Renderable> itr = renderables.iterator();
while (itr.hasNext()) {
Renderable r = itr.next();
r.render(batch);
}

batch.end();


if (ModelScreen.world != null) {
d.render(ModelScreen.world, cam.combined);
}


sr.setProjectionMatrix(cam.combined);
sr.begin(ShapeType.Line);
for (Renderable r : renderables) {
r.debug(sr);
}
sr.end();

// RENDER GUI
float camzoom = cam.zoom;
Vector3 camPos = cam.position.cpy();

batch.setProjectionMatrix(new Matrix4());
if (stage != null) {
batch.begin();

stage.draw();
batch.end();
}




}

public synchronized void add(Renderable renderable) {
renderables.add(renderable);
}

public synchronized void remove(Renderable renderable) {
renderables.remove(renderable);
}

public void resize(int width, int height) {
cam.viewportHeight = height;
cam.viewportWidth = width;
}

/**
* @param camControll the camControll to set
*/
public void setCamControll(CameraController camControll) {
this.camControll = camControll;
}

/**
* @param stage the stage to set
*/
public void setStage(Stage stage) {
stage.setCamera(cam);
stage.setViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);

this.stage = stage;
}

}

最佳答案

您的问题是,您为舞台和游戏的其余部分设置了相同的cam。 libgdx Stage 有自己的相机。您应该使用它,因为这个相机不会四处移动。因此,您可以使用 cameraSpriteBatch 来渲染游戏,并使用 StagecamSpriteBatch 来渲染游戏上的 UI。只需确保在调用 stage.draw() 之前调用 spriteBatch.end();,因为如果两个 SpriteBatch 处于 Activity 状态,就会把事情弄乱。

关于java - 为 Libgdx 制作随相机移动的 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22017994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com