9
\$\begingroup\$

My player HUD works perfect, but when I move, the monster name moves with me instead of getting attached to the monster.

Player has a HUD and I'm using Scene2D.ui to pull it off and works great.

I use unit scale, that's why I got 2 camera. One for UI stuff and other for game.

 public void update(float deltaTime) {
    // update creatures
    world.getPlayer().onThink(deltaTime);
    for (Monster monster : world.getMonsters()) {
        monster.onThink(deltaTime);
    }

    // update camera to the player
    camera.position.set(world.getPlayer().getPosition(), 0);
    camera.update();
}

@Override
public void render(float deltaTime) {
    // clear screen
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // seprate update logic
    update(deltaTime);

    // set camera to what we see
    worldRenderer.renderer.setView(camera);
    worldRenderer.renderer.render();

    // draw world
    game.spriteBatch.setProjectionMatrix(camera.combined);
    worldRenderer.render();

    // monster UI
    game.spriteBatch.setProjectionMatrix(uiCam.combined);
    for (Monster monster : world.getMonsters()) {
        uiCam.project(uiPosition.set(monster.getPosition().x, monster.getPosition().y, 0));
    }

    game.spriteBatch.begin();
    Assets.font.draw(game.spriteBatch, "Firefox", uiPosition.x / 2, uiPosition.y / 2);
    game.spriteBatch.end();

    // Player Hud
    game.spriteBatch.setProjectionMatrix(hud.stage.getCamera().combined);
    hud.stage.draw();
}

I'll appricate all kind of suggestions! If i could use Scene2D.ui for monsters aswell, would be great.

Cheers.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ You seem to be rendering the player text correctly, so why not just render the monster text the same way you render the player text? \$\endgroup\$
    – Charanor
    Jul 15, 2016 at 15:59
  • \$\begingroup\$ I'm using a HUD that i send to the player. Since it's one player and one labelText it was not hard to pull off. But monsters there will be alot of them, then i have to create alot of labelText variables and also no idea if this is a good way to do it :/ \$\endgroup\$
    – Printer
    Jul 15, 2016 at 16:03
  • \$\begingroup\$ One quick thing I noted is that you do 1 * world.unitScale. Try do just do world.unitScale since multiplying by one does nothing anyways and might be converting your float to an integer. \$\endgroup\$
    – Charanor
    Jul 15, 2016 at 16:07
  • \$\begingroup\$ Hehe, i just noticed that. But the font is to big if i going to be honest :P But i have no idea if it's a good way to render font on that way. Since it gets weird when the monster moves. \$\endgroup\$
    – Printer
    Jul 15, 2016 at 16:16

1 Answer 1

0
\$\begingroup\$

If you want the text to show up above the monster's head, you'll have to add at least the monster's sprite's height to the base Y of the font, something like:

Assets.font.draw(spriteBatch, "Monster", position.x, position.y + getFrame().getHeight());

Secondly, if you put the font rendering code before the sprite/texture rendering code, the font will be overlapped by the sprite, so I'd consider changing the order of the render lines to something like:

public void render(SpriteBatch spriteBatch) {
    spriteBatch.draw(getFrame(), position.x, position.y, width * world.unitScale, height * world.unitScale);
    Assets.font.draw(spriteBatch, "Monster", position.x, position.y);
    Assets.font.getData().setScale(1 * world.unitScale, 1 * world.unitScale);
}

This will make the font overlap the sprite, though I'd consider putting the name rendering code in a separate loop all together, otherwise monster sprites standing above your sprite but "under" the name tag will sometimes overlap it, but this is a design decision.

\$\endgroup\$
9
  • \$\begingroup\$ The problem is that unit scale, will cause the font be to big.so i need i seprate camera for ui stuff. But when i create one and draw the font, the font follow the player instead of stickning to the monster. \$\endgroup\$
    – Printer
    Jul 21, 2016 at 7:36
  • \$\begingroup\$ Does the camera follow the player? \$\endgroup\$
    – Jonathan
    Jul 21, 2016 at 12:00
  • \$\begingroup\$ Yes, ive changed. Now i got a gamecam which using unit scale and then i have ui cam. That should display monster name. Now it render the font perfect, but i need attacht the name to the monster, since when i move the text move with me. Instead for the monster \$\endgroup\$
    – Printer
    Jul 21, 2016 at 12:06
  • \$\begingroup\$ I've updated the post. With new codes and info \$\endgroup\$
    – Printer
    Jul 21, 2016 at 12:14
  • \$\begingroup\$ The monster's label should render on the game cam, not the UI cam. The UI cam will follow the player. Use the same camera you render the monster with to render its label, or a tertiary camera to render the monster's labels if you want to separate the monster's camera from the label camera. \$\endgroup\$
    – Jonathan
    Jul 21, 2016 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .