Reputation: 433
I'm using a stage in my game screen. In this stage I have actors like the map of the game (where player can move, and the camera of the stage moves along and scales (or relocates) to keep the viewport inside the map), the player and other game stuff. Then I add a HUD element that I make by extending Group:
public class Hud extends Group implements Disposable {
In this class I define a new OrthographicCamera:
hudCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
I want to use this camera to keep the HUD stationary on the screen. I do the drawing like this:
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.setProjectionMatrix(hudCamera.combined);
//Then draw my HUD stuff
}
The drawing works fine, but the problem is the (touch- and other) events. They have the wrong coordinates. How can I resolve this. I have tried to override the hit(float x, float y, boolean touchable) method and thought about overriding the localToParentCoordinates and parentToLocalCoordinates methods. But what would I put in them (multiplying by hudCamera.combined like when drawing, or something like that)? Or am I thinking this all wrong alltogether?
I'm also worried that, when I use zoom in my stage's camera, it will mess up with the coordinates. Is this a reality?
Upvotes: 1
Views: 5261
Reputation: 9823
I think camera.unproject(touchpos)
should work, but I have never used it. You would have to search for it.
I would suggest using 2 stages, as the game and the HUD are independent parts of the screen. By using 2 Stage
s you have 2 cameras, 1 for each Stage
. You can then move the game's camera
around and the HUD would still be at the same position.
Upvotes: 2
Reputation: 2560
For creating HUDs, you could generally use multiple Stages on your screen, which are being rendered on top of each other. You can even have more than two, e.g. for a Pause-Dialog which is rendered on top of the HUD when the Application is paused and resumed...
You just need to make sure that within the render()-method they are drawn an in the right order:
public void render(float delta) {
scene.act(delta);
hud.act(delta);
pauseDialog.act(delta);
scene.draw();
hud.draw();
pauseDialog.draw();
}
This way you can create different coordinate systems however you like. Especially when working with Fonts and Labels in your HUD, it should have a coordinate system that matches the pixel size, e.g. setViewport(1280,768,true);
or alike, whereas your Scene can have a completely different one.
Upvotes: 13