0
\$\begingroup\$

Right now I am working on a UI for my game. I'm adding my own UI elements to a group and adding that to a stage. But the listeners I'm adding to the actors are not firing off at all. I have looked on some StackExchange posts for answers but I couldn't find a good answer to my problem.

Right now the logic flow is Stage <-- Group <-- Custom Actors(FurnaceUI & FurnaceRecipeUI).

This is the HUD class (hastebin since it wouldnt paste right): https://hastebin.com/zelibejopa.java

FurnaceUI:

public class FurnaceUI extends Actor {

private final Texture uiBackground;

public FurnaceUI(Group group, float x, float y, float width, float height){
    group.addActor(this);
    this.setX(x);
    this.setY(y);
    this.setWidth(width);
    this.setHeight(height);
    this.uiBackground = new Texture(Gdx.files.internal("ui/furnaceui.png"));
    for(FurnaceRecipe recipe : FurnaceRecipeRegistry.getRecipes()){
        group.addActor(new FurnaceRecipeUI(recipe, this.getX() + 10, this.getY() + 490, 280, 100));
    }

    setBounds(x,y, width, height);
    setTouchable(Touchable.enabled);

    addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("usjkmd");
            return super.touchDown(event, x, y, pointer, button);
        }
    });

}

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    batch.draw(uiBackground, this.getX(), this.getY(), this.getWidth(), this.getHeight());
}

FurnaceRecipeUI:

public class FurnaceRecipeUI extends Actor {

private final FurnaceRecipe recipe;
private final static Texture texture = new Texture(Gdx.files.internal("ui/furnace_recipe_ui.png"));
private final static Texture arrowTexture = new Texture(Gdx.files.internal("ui/ui_arrow.png"));
int i = 0;

public FurnaceRecipeUI(FurnaceRecipe recipe, float x, float y, float width, float height){
    this.recipe = recipe;

    this.setX(x);
    this.setY(y);
    this.setWidth(width);
    this.setHeight(height);

    setBounds(x,y, width, height);
    setTouchable(Touchable.enabled);
    addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("aight");
            return super.touchDown(event, x, y, pointer, button);
        }

        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            System.out.println(keycode);
            return true;
        }
    });
}

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    batch.draw(texture, this.getX(), this.getY(), this.getWidth(), this.getHeight());


    for(Map.Entry<Item, Integer> result :  recipe.getIngredients().entrySet()){
        batch.draw(result.getKey().getTexture(), this.getX() + 10 + (i * 58), this.getY() + 20, 64, 64);
        i++;
    }
    i = 0;

    batch.draw(arrowTexture, this.getX() + 110, this.getY() + 40, 64, 32);
    batch.draw(recipe.getOutput().getTexture(), this.getX() + 200, this.getY() + 20, 64, 64);
}}

Then here is the main class of my game with the input multiplexer: https://hastebin.com/donosavoru.java

\$\endgroup\$
2
  • \$\begingroup\$ Sorry if this sounds dumb, but I haven't worked with LibGDX for a while. Don't you need to call an InputListener method inside the main loop, or does LibGDX automatically call that? If there does need to be something in the main loop, could you please verify it's there? \$\endgroup\$
    – Plug
    Commented Sep 2, 2018 at 14:38
  • \$\begingroup\$ No you don't have to call it inside the main loop if im right. I read the scene2d wiki on libgdx's github and they told me that if I use the Stage as a hud then I can use an input multiplexer to handle the events aswel. I added a hastebin link to be sure of my statement. \$\endgroup\$
    – Martacus
    Commented Sep 2, 2018 at 14:47

1 Answer 1

0
\$\begingroup\$

I found the answer. I was using the same event somewhere else and was always returning true. Which makes the event not fire on any other event listeners that are in the inputMultiplexer.

    @Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if (button == 0) {
        PlayerComponent component = player.getComponent(PlayerComponent.class);
        if (component.getPlayMode() == PlayerComponent.PlayMode.BUILD) {
            BuildingManager.getInstance().place(player);
        }
    } else if (button == 1) {
        PlayerComponent component = player.getComponent(PlayerComponent.class);
        if (component.getPlayMode() == PlayerComponent.PlayMode.BUILD) {
            component.setPlayMode();
        }
    }
    return true;
}

You see it always returns true. Now if I have another event listener in the same multiplexer returning true will tell it that it does not need to handle the event with other listeners.

\$\endgroup\$

You must log in to answer this question.

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