为什么监听器没有绑定到我分配给它的对象?
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]);
}
}
我正在为每个 Actor 分配一个侦听器。但是无论我点击哪个空白字段,每次点击的字段对我来说都是一样的,尽管每个 Actor 都是不同的。
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
我是不是做错了什么?因为据我了解,每个演员都应该有自己的听众,而不是所有人都是同一个听众。
尝试直接将 targetField
添加到侦听器并使用构造函数对其进行初始化。
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...
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]);
}
}
我正在为每个 Actor 分配一个侦听器。但是无论我点击哪个空白字段,每次点击的字段对我来说都是一样的,尽管每个 Actor 都是不同的。
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
我是不是做错了什么?因为据我了解,每个演员都应该有自己的听众,而不是所有人都是同一个听众。
尝试直接将 targetField
添加到侦听器并使用构造函数对其进行初始化。
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...