Search code examples
javalibgdxlistenerevent-listenerhashcode

Why isn't the listener bound to the object I am assigning it to?


for (int i = 0; i < mapSize[0]; i++) {
    for (int j = 0; j < mapSize[1]; j++) {
        hashCodes[i][j] = new Image(emptyField);
        int[] targetField = new int[2];
        targetField[0] = i;
        targetField[1] = j;
        hashCodes[i][j].addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("Empty Field clicked" + Arrays.toString(targetField));
                for (Image[] imageRow : hashCodes) {
                    for (Image actualImage : imageRow) {
                        System.out.print(actualImage.hashCode() + " ");
                    }
                    System.out.println();
                }
                if (MarvelousMashup.useInfinityStone) {
                    MarvelousMashup.useInfinityStone = false;
                    MainMenu.client.getMessageHandler().sendUseInfinityStoneRequest
                            (MarvelousMashup.activeCharacter,
                                    getActualEntity(MarvelousMashup.activeCharacter).getPosition(),
                                    targetField,
                                    getStone(MarvelousMashup.useInfinityStoneID));
                }
            }
        });
        tempField.addActor(hashCodes[i][j]);
    }
}

I am assigning a listener to each Actor. But no matter what empty field I click the clicked field is still the same for me every time although each Actor is a different one.

Empty Field clicked[5, 5]
817686795 1047478056 2115628016 611643685 1935122449 872826668 
91323447 110053477 1209411469 1320105604 1076984738 1006751649 
1171434979 1920907467 970535245 194481424 1534755892 1548081008 
753321708 464400749 343563528 1719072416 2092801316 376635015 
1344697180 28597262 300983713 284686302 1940445711 1356840024 
1142347343 1810923540 1581078471 1932332324 33233312 1860491691 

Am I doing something wrong? Because as far as I understand it, each actor should get their own listener and not all of them the same listener.


Solution

  • Try adding the targetField to the listener directly and initializing it with a constructor.

    for (int i = 0; i < mapSize[0]; i++) {
        for (int j = 0; j < mapSize[1]; j++) {
            hashCodes[i][j] = new Image(emptyField);
            hashCodes[i][j].addListener( new MyInputListener( i, j ) );
    // rest of code...
    
    
    class MyInputListener extends InputListener {
        int [] targetField = new int[2];
        InputListener( int i, int j ) {
          targetField[0] = i;
          targetField[1] = j;
        }
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
           return true;
        }
    
        @Override
        public void touchUp(  // etc...