LibGDX 使用 ClickListener 调整屏幕大小
LibGDX Screen Resize with ClickListener
我正在尝试使用 Java 和 LibGDX 创建一个简单的平台游戏。
但是,每当我调整菜单屏幕大小时,我的 ClickListeners 就会出现问题。 ClickListeners 未 'listening' 在屏幕的正确位置。
我觉得 camera.unproject();
可能有用,但我不知道在哪里实施它。
我在 Stack Overflow 上查看了很多问题,但没有找到有效的答案。
这是我的代码:
package uk.threepp.scrollfusion.screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import uk.threepp.scrollfusion.Const;
import uk.threepp.scrollfusion.ScrollFusionMain;
import uk.threepp.scrollfusion.transition.Animation;
import uk.threepp.scrollfusion.transition.TransitionScreenScene2D;
public class ScreenMenuMain extends TransitionScreenScene2D {
//region Variable Declarations
private ScrollFusionMain game;
private Stage stage;
private OrthographicCamera cam;
private Table menuTable;
private Music music;
private Skin menuSkin;
private TextButton
buttonPlay,
buttonExit;
//endregion
public ScreenMenuMain(final ScrollFusionMain g) {
game = g;
cam = new OrthographicCamera();
cam.setToOrtho(false, Const.SCREEN_WIDTH, Const.SCREEN_HEIGHT);
game.batch.setProjectionMatrix(cam.combined);
stage = new Stage(
new StretchViewport(
1280, 720, cam),
game.batch);
TextureAtlas atlas = new TextureAtlas("ui/MenuButton.pack");
menuSkin = new Skin(Gdx.files.internal("ui/MenuButton.json"), atlas);
menuTable = new Table(menuSkin);
menuTable.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
buttonPlay = new TextButton("Play", menuSkin);
buttonExit = new TextButton("Exit", menuSkin);
Label title = new Label("Scroll Fusion", menuSkin);
//region Button Listeners
buttonPlay.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
//TransitionHandler.transitionFadeOutToScreen(stage, Const.TRANSITION_SPEED, game, new ScreenGame(game));
game.setScreen(new ScreenGame(game));
dispose();
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonPlay.setColor(0, 1, 0, 1);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonPlay.setColor(1, 1, 1, 1);
}
});
buttonExit.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonExit.setColor(1, 0, 0, 1);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonExit.setColor(1, 1, 1, 1);
}
});
//endregion
menuTable.add(title).padBottom(40).row();
menuTable.add(buttonPlay).row();
menuTable.add(buttonExit).row();
menuTable.setFillParent(true);
stage.addActor(menuTable);
music = Gdx.audio.newMusic(Gdx.files.internal("music/Music01.wav"));
Gdx.input.setInputProcessor(stage);
}
//region Methods from Screen Interface
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
transitionUpdate(delta);
Gdx.app.log("FPS", String.valueOf(1/delta));
menuTable.getCell(buttonPlay);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
menuTable.setClip(true);
menuTable.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
@Override
public void show() {
music.setLooping(true);
music.play();
addTransition(Animation.fadeIn, Const.TRANSITION_SPEED);
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
stage.dispose();
music.dispose();
menuSkin.dispose();
}
//endregion
}
在您的 resize()
方法中,尝试在 menuTable.setSize()
之后而不是之前更新舞台视口。
我最近解决了这个问题
IntelliJ IDEA 只需要更新一下。如果您遇到这种情况,请修改您的 build.gradle
文件(例如添加一个随机换行符然后将其删除)。
然后软件会提示重新同步您的 gradle
项目文件;单击 'Yes'。
我正在尝试使用 Java 和 LibGDX 创建一个简单的平台游戏。
但是,每当我调整菜单屏幕大小时,我的 ClickListeners 就会出现问题。 ClickListeners 未 'listening' 在屏幕的正确位置。
我觉得 camera.unproject();
可能有用,但我不知道在哪里实施它。
我在 Stack Overflow 上查看了很多问题,但没有找到有效的答案。
这是我的代码:
package uk.threepp.scrollfusion.screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import uk.threepp.scrollfusion.Const;
import uk.threepp.scrollfusion.ScrollFusionMain;
import uk.threepp.scrollfusion.transition.Animation;
import uk.threepp.scrollfusion.transition.TransitionScreenScene2D;
public class ScreenMenuMain extends TransitionScreenScene2D {
//region Variable Declarations
private ScrollFusionMain game;
private Stage stage;
private OrthographicCamera cam;
private Table menuTable;
private Music music;
private Skin menuSkin;
private TextButton
buttonPlay,
buttonExit;
//endregion
public ScreenMenuMain(final ScrollFusionMain g) {
game = g;
cam = new OrthographicCamera();
cam.setToOrtho(false, Const.SCREEN_WIDTH, Const.SCREEN_HEIGHT);
game.batch.setProjectionMatrix(cam.combined);
stage = new Stage(
new StretchViewport(
1280, 720, cam),
game.batch);
TextureAtlas atlas = new TextureAtlas("ui/MenuButton.pack");
menuSkin = new Skin(Gdx.files.internal("ui/MenuButton.json"), atlas);
menuTable = new Table(menuSkin);
menuTable.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
buttonPlay = new TextButton("Play", menuSkin);
buttonExit = new TextButton("Exit", menuSkin);
Label title = new Label("Scroll Fusion", menuSkin);
//region Button Listeners
buttonPlay.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
//TransitionHandler.transitionFadeOutToScreen(stage, Const.TRANSITION_SPEED, game, new ScreenGame(game));
game.setScreen(new ScreenGame(game));
dispose();
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonPlay.setColor(0, 1, 0, 1);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonPlay.setColor(1, 1, 1, 1);
}
});
buttonExit.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonExit.setColor(1, 0, 0, 1);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonExit.setColor(1, 1, 1, 1);
}
});
//endregion
menuTable.add(title).padBottom(40).row();
menuTable.add(buttonPlay).row();
menuTable.add(buttonExit).row();
menuTable.setFillParent(true);
stage.addActor(menuTable);
music = Gdx.audio.newMusic(Gdx.files.internal("music/Music01.wav"));
Gdx.input.setInputProcessor(stage);
}
//region Methods from Screen Interface
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
transitionUpdate(delta);
Gdx.app.log("FPS", String.valueOf(1/delta));
menuTable.getCell(buttonPlay);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
menuTable.setClip(true);
menuTable.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
@Override
public void show() {
music.setLooping(true);
music.play();
addTransition(Animation.fadeIn, Const.TRANSITION_SPEED);
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
stage.dispose();
music.dispose();
menuSkin.dispose();
}
//endregion
}
在您的 resize()
方法中,尝试在 menuTable.setSize()
之后而不是之前更新舞台视口。
我最近解决了这个问题
IntelliJ IDEA 只需要更新一下。如果您遇到这种情况,请修改您的 build.gradle
文件(例如添加一个随机换行符然后将其删除)。
然后软件会提示重新同步您的 gradle
项目文件;单击 'Yes'。