我在滚动窗格中创建了一个包含许多按钮(Actors)的表,如下面的代码所示:
public class ChooseLevel implements Screen {
private Stage stage;
private Button[][] btnLevel;
private Skin skin;
private Table table;
private ScrollPane scrollPane;
private ScrollPaneStyle paneStyle;
@Override
public void show() {
stage = new Stage(new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
Gdx.input.setInputProcessor(stage);
skin = new Skin(new TextureAtlas(Gdx.files.internal("pack.pack")));
table = new Table();
table.padTop(100f);
btnLevel = new Button[50][3];
for (int row = 0; row < 50; row++) {
for (int col = 0; col < 3; col++) {
btnLevel[row][col] = new Button(skin.getDrawable("up"), skin.getDrawable("down"));
table.add(btnLevel[row][col]).pad(10f);
if (col == 2) table.row().pad(10f);
btnLevel[row][col].addListener(new ClickListener() {
@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) {
for (int row = 0; row < btnLevel.length; row++) {
for (int col = 0; col < btnLevel[0].length; col++) {
if (btnLevel[row][col].isPressed()) {
((Game) Gdx.app.getApplicationListener()).setScreen(new Play());
}
}
}
}
});
}
}
paneStyle = new ScrollPaneStyle(null, null, null, null, skin.getDrawable("unPressed"));
paneStyle.vScrollKnob.setMinWidth(10f);
scrollPane = new ScrollPane(table, paneStyle);
scrollPane.setBounds(0, 0, stage.getWidth(), stage.getHeight());
stage.addActor(scrollPane);
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, false);
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
问题是:当我向上或向下滚动时,按钮会听到触摸,然后在按下特定按钮之前调用此行((Game) Gdx.app.getApplicationListener()).setScreen(new Play());
。
答案 0 :(得分:0)
更改此行:
if (btnLevel[row][col].isPressed())
到
if (btnLevel[row][col].isChecked())