我想知道是否有一种方法可以使用组合在一起的单击监听器来设置多个图像。我有这个图像数组,我试图使用增强的for each循环,但它不起作用。我尝试在一个数组中的所有图像上设置一个单击侦听器,而不是使用单击侦听器单独设置该数组的每个成员。
编辑
public void Player1() {
deck.displayHand();
stage = new Stage(new ScreenViewport());
group.addActor(deck.hand.get(0));
deck.hand.get(0).setPosition(200,0);
deck.hand.get(0).setTouchable(Touchable.enabled);
group.addActor(deck.hand.get(1));
deck.hand.get(1).setPosition(220,0);
deck.hand.get(1).setTouchable(Touchable.enabled);
group.addActor(deck.hand.get(2));
deck.hand.get(2).setPosition(240,0);
deck.hand.get(2).setTouchable(Touchable.enabled);
stage.addActor(group);
rand = (int) (Math.random() * (deck.hand.size));
for (int z = 0; z<deck.hand.size; z++){
final int finalZ = z;
deck.hand.get(z).addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
deck.hand.get(finalZ).addAction(Actions.moveTo(300,400));
}
});
}
}
@Override
public void draw(Batch batch, float parentAlpha) {
Gdx.input.setInputProcessor(stage);
stage.act();
stage.draw();
}}
我是否必须使用增强的for each循环,或者是否可以使用不同的循环?目的是让用户点击/触摸其中一个图像,它将转到屏幕的一个位置,对于每个点击/触摸的图像,它将转到屏幕上的一个位置。
发布于 2018-08-26 19:12:14
如果这是一种类似于拼图的东西,你可以使用一个计数器来递增每个图像的大小,并将其作为位置。这没有什么意义,所以在这里:
// Fill array with each image in it's grid position e.g. the bottom left image would go at (0, 0) in the array.
Image[][] array = new Image[cols][rows];
// All the images have to be the same size but not necessarily square.
imageWidth = 100;
imageHeight = 100;
for(int y = 0; y < array.length; y++) {
for(int x = 0; x < array[0].length; x++) {
Image img = array[y][x];
img.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
img.addAction(Actions.moveTo(x * imageWidth + initialXOffset, y * imageHeight + initialYOffset));
}
});
}
}
仅当图像大小完全相同时,才能使用。
https://stackoverflow.com/questions/52020728
复制相似问题