libgdxscene2dclicklistener

ClickListener enter/exit events not working properly Libgdx


I am creating a Table that contains multiple actors. The table also contains an Image which has a ClickListener attached. From other actors I draw an arrow when they are tapped or clicked, arrow that can target the Image from the Table. I want to know when the arrow enters and exits the Image from the Table. The problem is that the enter event doesn't always trigger, or it triggers only from a portion of the image or exit gets triggered as soon as enter was triggered. Any idea what could be wrong?

oponentHand = new Table();
        for (int i = 0; i < 4; i++) {
            oponentHand.add().width(game.developingWidth / (11.5f)).height(game.developingHeight / (5.5f)).padLeft(5)
                    .expand();
        }
        Texture oponentTexture = new Texture(Gdx.files.internal("data/mihai.jpg"));
        Table tempTable = new Table();
        oponentPortrait = new Image(oponentTexture);
        oponentLife = new Label("Life", textsStyle);
        tempTable.add(oponentLife).row();
        oponentPortrait.setTouchable(Touchable.enabled);
        tempTable.add(oponentPortrait).expand().fill();
        oponentPortrait.addListener(new ClickListener() {
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                super.enter(event, x, y, pointer, fromActor);
                isOnTarget = true;
                targetedCardType = "enemyHero";
                System.out.println("Entering enemy hero");
            }

            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                super.exit(event, y, y, pointer, toActor);
                if (pointer == -1) {
                    isOnTarget = false;
                    System.out.println("Exiting enemy hero");
                }
            }
        });
        Texture oponentPowerTexture1 = new Texture(Gdx.files.internal("data/powSword.png"));
        Texture oponentPowerTexture2 = new Texture(Gdx.files.internal("data/powShield.png"));
        Image firstOponentPower = new Image(oponentPowerTexture1);
        Image secondOponentPower = new Image(oponentPowerTexture2);
        oponentHand.add(firstOponentPower).width(game.developingWidth / (11.5f)).height(game.developingHeight / (6))
                .padLeft(5);
        oponentHand.add(tempTable).width(game.developingWidth / (11.5f)).height(game.developingHeight / (6)).fill().padLeft(5);
        oponentHand.add(secondOponentPower).width(game.developingWidth / (11.5f)).height(game.developingHeight / (6))
                .padLeft(5);
        for (int i = 0; i < 4; i++) {
            oponentHand.add().width(game.developingWidth / (11.5f)).height(game.developingHeight / (6)).padLeft(5)
                    .expand();
        }

Solution

  • I found the problem that was causing the enter/exit events not to trigger. I was adding another actor to the stage later in the game and by coincidence it happened to be added on top of the image on which I was trying to verify the enter/exit events. I was fading in and then fading out the actor that I was adding to the stage by using Action so it was only visible for a very short amount of time. The problem was that I was never removing it from the stage, because fade out only changes the alpha of the actor, so it wasn't allowing the image below it to get the events. I solved it by removing it from the stage after fading it out using Actions.removeActor().