9

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.

Vaillancourt
  • 16,325
  • 17
  • 55
  • 61
Printer
  • 317
  • 2
  • 11
  • 2
    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? – Charanor Jul 15 '16 at 15:59
  • 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 :/ – Printer Jul 15 '16 at 16:03
  • 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. – Charanor Jul 15 '16 at 16:07
  • 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. – Printer Jul 15 '16 at 16:16

1 Answers1

0

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.

Jonathan
  • 1
  • 1
  • 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. – Printer Jul 21 '16 at 07:36
  • Does the camera follow the player? – Jonathan Jul 21 '16 at 12:00
  • 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 – Printer Jul 21 '16 at 12:06
  • I've updated the post. With new codes and info – Printer Jul 21 '16 at 12:14
  • 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. – Jonathan Jul 21 '16 at 13:01
  • Problem with that is, i'm using unit scale to render monster, player and map. Which will cause the font being big as hell. Then when i shrink it down, it cause other problems such as do not render whole text. – Printer Jul 21 '16 at 13:31
  • Have you considered an extra camera just for rendering the text? A camera that follows the creature camera should work. – Jonathan Jul 22 '16 at 01:54
  • Thats how i'm doing currently. But it seems like the text move with the player. Instead for being attached to the monster. – Printer Jul 22 '16 at 07:30
  • Now i have this problem :/ – adam Jun 18 '17 at 19:15