我定义了一个mainmenu类,我试图在表格中显示一些图像,并让它们响应触摸,但是当我在主屏幕的构造函数中定义调用类(只是调用它)时,控件不要&#虽然我以定义控件类的相同方式定义了mainmenu类,但我没有回应。
(注意当我在主屏幕内使用mainmenu对象时,它会渲染图像和控件,但它们不会响应任何点击)。
当我打电话给主菜单时,主要问题就出现了,游戏正在运行,但控件不会响应触摸。这是两个类的代码:
public class Controller {
public Viewport viewport;
public Stage stage;
public boolean upPressed;
public boolean leftPressed;
public boolean rightPressed;
public boolean pausePressed;
public static boolean visiblity;
public Image buttonUp;
public Image buttonDown;
public Image buttonLeft;
public Image buttonRight;
public Image buttonpause;
public OrthographicCamera camera;
public Table table;
public Table table1;
public Table table2;
//Constructor.
public Controller(SpriteBatch spriteBatch) {
camera = new OrthographicCamera();
viewport = new FitViewport(Fruits.V_WIDTH, Fruits.V_HIEGT, camera);
stage = new Stage(viewport, spriteBatch);
Gdx.input.setInputProcessor(stage);
visiblity=false;
//Buttons with images.
buttonUp = new Image(new Texture("Pause.png"));
buttonUp.setSize(30, 30);
buttonUp.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
upPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
upPressed = false;
}
});
buttonLeft = new Image(new Texture("buttonleft.png"));
buttonLeft.setSize(65, 65);
buttonLeft.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = false;
}
});
buttonRight = new Image(new Texture("buttonright.png"));
buttonRight.setSize(65, 65);
buttonRight.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = false;
}
});
buttonpause = new Image(new Texture("pausing.png"));
buttonpause.setSize(30, 30);
buttonpause.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = false;
}
});
//Table with buttons.
table = new Table();
table.left().bottom(); //Align to the left bottom.
table.add(buttonLeft).size(buttonLeft.getWidth(), buttonLeft.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
// table.add(buttonUp).size(buttonUp.getWidth(), buttonUp.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
table.add(buttonRight).size(buttonRight.getWidth(), buttonRight.getHeight());
stage.addActor(table);
table1=new Table();
table1.right().top();
table1.setFillParent(true);
//table1.add();
table1.row().pad(25, 3, 0, 10);
table1.add(buttonUp).size(buttonUp.getWidth(), buttonUp.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
stage.addActor(table1);
table2=new Table();
table2.center();
table2.setFillParent(true);
//table1.add();
table2.row().pad(25, 3, 0, 10);
table2.add(buttonpause).size(buttonpause.getWidth(), buttonpause.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
stage.addActor(table2);
}
public void draw() {
stage.draw();
}
public void resize(int width, int height) {
viewport.update(width, height);
}
public boolean isUpPressed() {
return upPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public boolean isPausePressed() {
return pausePressed;
}
}
mainmenu的代码
public class MainMenu{
/* public Viewport viewport;
public Stage stage;
public boolean pausePressed;
public boolean resumePressed;
public boolean exitPressed;
public Image buttonpause;
public Image buttonDown;
public Image buttonresume;
public Image buttonexit;
public OrthographicCamera camera;
public Table table;
public Table table2;
//Constructor.
public MainMenu(SpriteBatch spriteBatch) {
camera = new OrthographicCamera();
viewport = new FitViewport(Fruits.V_WIDTH, Fruits.V_HIEGT, camera);
stage = new Stage(viewport, spriteBatch);
Gdx.input.setInputProcessor(stage);
//Buttons with images.
buttonpause = new Image(new Texture("pausing.png"));
buttonpause.setSize(30, 30);
buttonpause.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
pausePressed = false;
}
});
buttonresume = new Image(new Texture("resume.png"));
buttonresume.setSize(65, 65);
buttonresume.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
resumePressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
resumePressed = false;
}
});
buttonexit = new Image(new Texture("exit.png"));
buttonexit.setSize(65, 65);
buttonexit.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
exitPressed = true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
exitPressed = false;
}
});
//Table with buttons.
table2=new Table();
table2.center();
table2.setFillParent(true);
//table2.add();
table2.row().pad(25, 3, 0, 10);
table2.add(buttonpause).size(buttonpause.getWidth(), buttonpause.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
table2.row().pad(25, 3, 0, 10);
table2.add(buttonresume).size(buttonresume.getWidth(), buttonresume.getHeight());
table2.row().pad(25, 3, 0, 10);
table2.add(buttonexit).size(buttonexit.getWidth(), buttonexit.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
stage.addActor(table2);
}
public void draw() {
stage.draw();
}
public void resize(int width, int height) {
viewport.update(width, height);
}
public boolean isPausePressed() {
return pausePressed;
}
public boolean isResumePressed() {
return resumePressed;
}
public boolean isExitPressed() {
return exitPressed;
}
*/
}