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...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句