diff --git a/.gitignore b/.gitignore index 03eff3c8..f5c8a719 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ lib/ *.hprof .DS_Store *.sav +/**/settings/keys.json ### STS ### .apt_generated diff --git a/core/src/main/java/com/gdx/game/common/Constats.java b/core/src/main/java/com/gdx/game/common/Constats.java index f64fc27a..5ac7438a 100644 --- a/core/src/main/java/com/gdx/game/common/Constats.java +++ b/core/src/main/java/com/gdx/game/common/Constats.java @@ -6,4 +6,7 @@ public class Constats { public static final String COURTESY_PHRASES_PATH = "conversations/conversation_courtesy.json"; + public static final String PARTIAL_CONTROLS_SETTINGS_PATH = "settings/keys.json"; + public static final String FULL_CONTROLS_SETTINGS_PATH = "core/src/main/resources/settings/keys.json"; + } diff --git a/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java b/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java new file mode 100644 index 00000000..ba879aee --- /dev/null +++ b/core/src/main/java/com/gdx/game/common/DefaultControlsMap.java @@ -0,0 +1,21 @@ +package com.gdx.game.common; + +import com.gdx.game.component.InputComponent; + +import java.util.HashMap; + +public class DefaultControlsMap { + + public static final HashMap DEFAULT_CONTROLS = new HashMap<>(); + + static { + DEFAULT_CONTROLS.put("33", InputComponent.Keys.INTERACT.name()); + DEFAULT_CONTROLS.put("43", InputComponent.Keys.OPTION.name()); + DEFAULT_CONTROLS.put("32", InputComponent.Keys.RIGHT.name()); + DEFAULT_CONTROLS.put("111", InputComponent.Keys.QUIT.name()); + DEFAULT_CONTROLS.put("51", InputComponent.Keys.UP.name()); + DEFAULT_CONTROLS.put("47", InputComponent.Keys.DOWN.name()); + DEFAULT_CONTROLS.put("29", InputComponent.Keys.LEFT.name()); + } + +} diff --git a/core/src/main/java/com/gdx/game/common/UtilityClass.java b/core/src/main/java/com/gdx/game/common/UtilityClass.java new file mode 100644 index 00000000..fc92c3eb --- /dev/null +++ b/core/src/main/java/com/gdx/game/common/UtilityClass.java @@ -0,0 +1,30 @@ +package com.gdx.game.common; + +import java.util.HashMap; +import java.util.Map; + +public class UtilityClass { + + public static HashMap mapInverter(HashMap hashMap){ + + HashMap newHashMap = new HashMap<>(); + + for(Map.Entry entry : hashMap.entrySet()){ + newHashMap.put(entry.getValue(), entry.getKey()); + } + + return newHashMap; + } + + public static Map mapInverter(Map hashMap){ + + Map newMap = new HashMap<>(); + + for(Map.Entry entry : hashMap.entrySet()){ + newMap.put(entry.getValue(), entry.getKey()); + } + + return newMap; + } + +} diff --git a/core/src/main/java/com/gdx/game/component/InputComponent.java b/core/src/main/java/com/gdx/game/component/InputComponent.java index 399e1b9c..6d20f265 100644 --- a/core/src/main/java/com/gdx/game/component/InputComponent.java +++ b/core/src/main/java/com/gdx/game/component/InputComponent.java @@ -7,13 +7,15 @@ import java.util.HashMap; import java.util.Map; +import static com.gdx.game.common.UtilityClass.mapInverter; + public abstract class InputComponent extends ComponentSubject implements Component, InputProcessor { protected Entity.Direction currentDirection = null; protected Entity.State currentState = null; protected Json json; - protected enum Keys { + public enum Keys { LEFT, RIGHT, UP, DOWN, QUIT, INTERACT, OPTION } @@ -51,4 +53,39 @@ public boolean scrolled(float amountX, float amountY) { return false; } + public static HashMap playerControls = new HashMap<>(); + + public static void setPlayerControlMapFromJsonControlsMap(HashMap jsonMap){ + + HashMap newPlayerControls = new HashMap<>(); + + for (var entry : jsonMap.entrySet()) { + newPlayerControls.put(Integer.valueOf(entry.getKey()), InputComponent.Keys.valueOf(entry.getValue())); + } + + playerControls = newPlayerControls; + + } + + public static HashMap mapJsonControlsToPlayerControl(HashMap playerControls){ + + HashMap result = new HashMap<>(); + + for (var entry : playerControls.entrySet()) { + result.put(entry.getKey().toString(), entry.getValue().toString()); + } + + return result; + } + + public static HashMap changeValueFromJsonControlsMap(HashMap jsonMap, + Keys keyValue, + Integer keyCode){ + jsonMap = mapInverter(jsonMap); + jsonMap.put(keyValue.name(), String.valueOf(keyCode)); + jsonMap = mapInverter(jsonMap); + + return jsonMap; + } + } diff --git a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java index 9721af06..4a9d7429 100644 --- a/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java +++ b/core/src/main/java/com/gdx/game/entities/player/PlayerInputComponent.java @@ -92,30 +92,31 @@ public void update(Entity entity, float delta) { @Override public boolean keyDown(int keycode) { - switch (keycode) { - case Input.Keys.DOWN: - case Input.Keys.S: + + if(!playerControls.containsKey(keycode)){ + return false; + } + + switch (playerControls.get(keycode)) { + case DOWN: this.downPressed(); break; - case Input.Keys.UP: - case Input.Keys.W: + case UP: this.upPressed(); break; - case Input.Keys.LEFT: - case Input.Keys.A: + case LEFT: this.leftPressed(); break; - case Input.Keys.RIGHT: - case Input.Keys.D: + case RIGHT: this.rightPressed(); break; - case Input.Keys.E: + case INTERACT: this.interactPressed(); break; - case Input.Keys.O: + case OPTION: this.optionPressed(); break; - case Input.Keys.ESCAPE: + case QUIT: this.quitPressed(); break; default: @@ -126,35 +127,33 @@ public boolean keyDown(int keycode) { @Override public boolean keyUp(int keycode) { - switch (keycode) { - case Input.Keys.DOWN: - case Input.Keys.S: + + if(!playerControls.containsKey(keycode)){ + return false; + } + + switch (playerControls.get(keycode)) { + case DOWN: this.downReleased(); break; - case Input.Keys.UP: - case Input.Keys.W: + case UP: this.upReleased(); break; - case Input.Keys.LEFT: - case Input.Keys.A: + case LEFT: this.leftReleased(); break; - case Input.Keys.RIGHT: - case Input.Keys.D: + case RIGHT: this.rightReleased(); break; - case Input.Keys.E: + case INTERACT: this.interactReleased(); break; - case Input.Keys.O: + case OPTION: this.optionReleased(); break; - case Input.Keys.ESCAPE: + case QUIT: this.quitReleased(); break; - case Input.Keys.BACKSPACE: - debug = !debug; - break; default: break; } diff --git a/core/src/main/java/com/gdx/game/screen/BaseScreen.java b/core/src/main/java/com/gdx/game/screen/BaseScreen.java index b7e672d1..1219c09b 100644 --- a/core/src/main/java/com/gdx/game/screen/BaseScreen.java +++ b/core/src/main/java/com/gdx/game/screen/BaseScreen.java @@ -7,8 +7,10 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.ui.TextField; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.viewport.Viewport; @@ -22,6 +24,8 @@ import java.util.ArrayList; import java.util.List; +import static com.gdx.game.manager.ResourceManager.skin; + public class BaseScreen implements Screen, AudioSubject { protected final GdxGame gdxGame; protected ResourceManager resourceManager; diff --git a/core/src/main/java/com/gdx/game/screen/GameScreen.java b/core/src/main/java/com/gdx/game/screen/GameScreen.java index 40088950..ab9eddf9 100644 --- a/core/src/main/java/com/gdx/game/screen/GameScreen.java +++ b/core/src/main/java/com/gdx/game/screen/GameScreen.java @@ -2,13 +2,14 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputMultiplexer; +import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; -import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.ScreenUtils; +import com.badlogic.gdx.utils.SerializationException; import com.gdx.game.GdxGame; import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; @@ -28,6 +29,13 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.HashMap; + +import static com.gdx.game.common.Constats.FULL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.Constats.PARTIAL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.DefaultControlsMap.DEFAULT_CONTROLS; +import static com.gdx.game.component.InputComponent.playerControls; +import static com.gdx.game.component.InputComponent.setPlayerControlMapFromJsonControlsMap; public class GameScreen extends BaseScreen implements ComponentObserver { @@ -56,14 +64,13 @@ public enum GameState { protected MapManager mapManager; protected OrthographicCamera camera; protected OrthographicCamera hudCamera; - private Stage gameStage = new Stage(); - private Json json; - private GdxGame game; - private InputMultiplexer multiplexer; + private final Json json; + private final GdxGame game; + private final InputMultiplexer multiplexer; - private Entity player; - private PlayerHUD playerHUD; + private final Entity player; + private final PlayerHUD playerHUD; private float startX; private float startY; @@ -92,6 +99,28 @@ public GameScreen(GdxGame gdxGame, ResourceManager resourceManager) { player = EntityFactory.getInstance().getEntity(ProfileManager.getInstance().getProperty("playerCharacter", EntityFactory.EntityType.class)); player.registerObserver(this); + //initialize controls + HashMap controlMap; + + if(playerControls.isEmpty()){ + try { + controlMap = json.fromJson(HashMap.class, Gdx.files.local(PARTIAL_CONTROLS_SETTINGS_PATH)); + + if (DEFAULT_CONTROLS.size() != controlMap.size()){ + throw new SerializationException("Not valid control map"); + } + } catch (SerializationException se) {LOGGER.error(se.getMessage()); + + // if I can not read the file , so use the default controls binding and save it + controlMap = DEFAULT_CONTROLS; + + FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); + commandsFile.writeString(json.prettyPrint(controlMap), false); + } + + setPlayerControlMapFromJsonControlsMap(controlMap); + } + mapManager.setPlayer(player); mapManager.setCamera(camera); @@ -236,7 +265,6 @@ public static GameState getGameState() { public static void setGameState(GameState state) { switch (state) { - case RUNNING -> gameState = GameState.RUNNING; case LOADING -> { ProfileManager.getInstance().loadProfile(); gameState = GameState.RUNNING; diff --git a/core/src/main/java/com/gdx/game/screen/OptionScreen.java b/core/src/main/java/com/gdx/game/screen/OptionScreen.java index 1f859480..73aaa6de 100644 --- a/core/src/main/java/com/gdx/game/screen/OptionScreen.java +++ b/core/src/main/java/com/gdx/game/screen/OptionScreen.java @@ -1,32 +1,49 @@ package com.gdx.game.screen; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; +import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Align; +import com.badlogic.gdx.utils.Json; +import com.badlogic.gdx.utils.SerializationException; import com.crashinvaders.vfx.VfxManager; import com.crashinvaders.vfx.effects.GaussianBlurEffect; import com.gdx.game.GdxGame; import com.gdx.game.audio.AudioManager; import com.gdx.game.audio.AudioObserver; +import com.gdx.game.component.InputComponent; +import com.gdx.game.manager.PreferenceManager; import com.gdx.game.manager.ResourceManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.HashMap; + +import static com.gdx.game.common.Constats.FULL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.Constats.PARTIAL_CONTROLS_SETTINGS_PATH; +import static com.gdx.game.common.DefaultControlsMap.DEFAULT_CONTROLS; +import static com.gdx.game.common.UtilityClass.mapInverter; +import static com.gdx.game.component.InputComponent.*; +import static com.gdx.game.manager.ResourceManager.skin; public class OptionScreen extends BaseScreen { private Table optionTable; private Table musicTable; private Table controlTable; - private Stage optionStage = new Stage(); - private Stage musicStage = new Stage(); - private Stage controlStage = new Stage(); - private Stage backgroundStage = new Stage(); - private BaseScreen previousScreen; + private final Stage optionStage = new Stage(); + private final Stage musicStage = new Stage(); + private final Stage controlStage = new Stage(); + private final Stage backgroundStage = new Stage(); + private final BaseScreen previousScreen; private Image previousScreenAsImg; private boolean musicClickListener; private boolean controlClickListener; @@ -35,6 +52,10 @@ public class OptionScreen extends BaseScreen { private VfxManager vfxManager; private GaussianBlurEffect vfxEffect; + private HashMap playerControlsNew = mapJsonControlsToPlayerControl(playerControls); + + private static final Logger LOGGER = LoggerFactory.getLogger(GameScreen.class); + public OptionScreen(GdxGame gdxGame, BaseScreen previousScreen, ResourceManager resourceManager) { super(gdxGame, resourceManager); this.previousScreen = previousScreen; @@ -68,24 +89,212 @@ private void loadContents() { private void handleControlButton() { createButton("Control",0, optionTable.getHeight()/10, optionTable); - Actor controlButton = optionTable.getCells().get(0).getActor(); + Actor controlButton = optionTable.getCells().get(optionTable.getCells().size - 1).getActor(); + controlButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { controlClickListener = true; + controlTable = createTable(); + handleControlSettings(); handleControlBackButton(); } }); } + private void handleControlSettings() { + + Json jsonObject = new Json(); + + if (playerControls.isEmpty()){ + try { + playerControlsNew = jsonObject.fromJson(HashMap.class, Gdx.files.internal(PARTIAL_CONTROLS_SETTINGS_PATH)); + + if (DEFAULT_CONTROLS.size() != playerControlsNew.size()) { + throw new SerializationException("Not valid control map"); + } + + } catch (SerializationException se) { + + LOGGER.error(se.getMessage()); + + playerControlsNew = DEFAULT_CONTROLS; + } + } + + + playerControlsNew = mapInverter(playerControlsNew); + + Label upLabel = new Label(InputComponent.Keys.UP.name(), skin); + TextField upText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.UP.name()))), skin); + Label downLabel = new Label(InputComponent.Keys.DOWN.name(), skin); + TextField downText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.DOWN.name()))), skin); + Label leftLabel = new Label(InputComponent.Keys.LEFT.name(), skin); + TextField leftText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.LEFT.name()))), skin); + Label rightLabel = new Label(InputComponent.Keys.RIGHT.name(), skin); + TextField rightText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.RIGHT.name()))), skin); + Label interactLabel = new Label(InputComponent.Keys.INTERACT.name(), skin); + TextField interactText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.INTERACT.name()))), skin); + Label optionLabel = new Label(InputComponent.Keys.OPTION.name(), skin); + TextField optionText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.OPTION.name()))), skin); + Label quitLabel = new Label(InputComponent.Keys.QUIT.name(), skin); + TextField quitText = new TextField( + Input.Keys.toString(Integer.parseInt(playerControlsNew.get(InputComponent.Keys.QUIT.name()))), skin); + + playerControlsNew = mapInverter(playerControlsNew); + + downText.setMaxLength(1); + upText.setMaxLength(1); + leftText.setMaxLength(1); + rightText.setMaxLength(1); + interactText.setMaxLength(1); + optionText.setMaxLength(1); + quitText.setMaxLength(1); + + downText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.DOWN, keycode); + + downText.setMaxLength(Input.Keys.toString(keycode).length()); + downText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + upText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.UP, keycode); + + upText.setMaxLength(Input.Keys.toString(keycode).length()); + upText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + leftText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.LEFT, keycode); + + leftText.setMaxLength(Input.Keys.toString(keycode).length()); + leftText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + rightText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.RIGHT, keycode); + + rightText.setMaxLength(Input.Keys.toString(keycode).length()); + rightText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + interactText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.INTERACT, keycode); + + interactText.setMaxLength(Input.Keys.toString(keycode).length()); + interactText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + optionText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.OPTION, keycode); + + optionText.setMaxLength(Input.Keys.toString(keycode).length()); + optionText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + quitText.addListener(new InputListener(){ + + @Override + public boolean keyDown(InputEvent event, int keycode) { + + playerControlsNew = changeValueFromJsonControlsMap(playerControlsNew, Keys.QUIT, keycode); + + quitText.setMaxLength(Input.Keys.toString(keycode).length()); + quitText.setText(Input.Keys.toString(keycode)); + + return super.keyDown(event, keycode); + } + }); + + controlTable.add(upLabel); + controlTable.add(upText); + controlTable.row(); + controlTable.add(downLabel); + controlTable.add(downText); + controlTable.row(); + controlTable.add(leftLabel); + controlTable.add(leftText); + controlTable.row(); + controlTable.add(rightLabel); + controlTable.add(rightText); + controlTable.row(); + controlTable.add(interactLabel); + controlTable.add(interactText); + controlTable.row(); + controlTable.add(optionLabel); + controlTable.add(optionText); + controlTable.row(); + controlTable.add(quitLabel); + controlTable.add(quitText); + controlTable.row(); + + } + private void handleControlBackButton() { createButton("Back",0, controlTable.getHeight()/5, controlTable); - Actor backButton = controlTable.getCells().get(0).getActor(); + Actor backButton = controlTable.getCells().get(controlTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { + + Json json = new Json(); + + FileHandle commandsFile = Gdx.files.local(FULL_CONTROLS_SETTINGS_PATH); + commandsFile.writeString(json.prettyPrint(playerControlsNew), false); + + setPlayerControlMapFromJsonControlsMap(playerControlsNew); + controlClickListener = false; } }); @@ -94,7 +303,7 @@ public void clicked(InputEvent even, float x, float y) { private void handleMusicButton() { createButton("Music",0, optionTable.getHeight()/15, optionTable); - Actor musicButton = optionTable.getCells().get(1).getActor(); + Actor musicButton = optionTable.getCells().get(optionTable.getCells().size - 1).getActor(); musicButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { @@ -107,16 +316,16 @@ public void clicked(InputEvent even, float x, float y) { } private void handleMusicSettings() { - Label musicLabel = new Label("MUSIC", resourceManager.skin); + Label musicLabel = new Label("MUSIC", skin); musicLabel.setAlignment(Align.left); - Slider musicSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); - musicSlider.setValue(gdxGame.getPreferenceManager().getMusicVolume()); + Slider musicSlider = new Slider(0, 1, 0.01f, false, skin); + musicSlider.setValue(PreferenceManager.getMusicVolume()); musicSlider.addListener(event -> { gdxGame.getPreferenceManager().setMusicVolume(musicSlider.getValue()); - AudioManager.getInstance().getCurrentMusic().setVolume(gdxGame.getPreferenceManager().getMusicVolume()); + AudioManager.getInstance().getCurrentMusic().setVolume(PreferenceManager.getMusicVolume()); return false; }); - CheckBox musicCheckbox = new CheckBox("Enable Music", resourceManager.skin); + CheckBox musicCheckbox = new CheckBox("Enable Music", skin); musicCheckbox.setChecked(gdxGame.getPreferenceManager().isMusicEnabled()); musicCheckbox.addListener(event -> { gdxGame.getPreferenceManager().setMusicEnabled(musicCheckbox.isChecked()); @@ -124,15 +333,15 @@ private void handleMusicSettings() { return false; }); - Label soundLabel = new Label("SOUND", resourceManager.skin); + Label soundLabel = new Label("SOUND", skin); soundLabel.setAlignment(Align.left); - Slider soundSlider = new Slider(0, 1, 0.01f, false, resourceManager.skin); + Slider soundSlider = new Slider(0, 1, 0.01f, false, skin); soundSlider.setValue(gdxGame.getPreferenceManager().getSoundVolume()); soundSlider.addListener(event -> { gdxGame.getPreferenceManager().setSoundVolume(soundSlider.getValue()); return false; }); - CheckBox soundCheckbox = new CheckBox("Enable Sound", resourceManager.skin); + CheckBox soundCheckbox = new CheckBox("Enable Sound", skin); soundCheckbox.setChecked(gdxGame.getPreferenceManager().isSoundEffectsEnabled()); soundCheckbox.addListener(event -> { boolean enabled = soundCheckbox.isChecked(); @@ -159,7 +368,7 @@ private void handleMusicSettings() { private void handleMusicBackButton() { createButton("Back",0, musicTable.getHeight()/20, musicTable); - Actor backButton = musicTable.getCells().get(6).getActor(); + Actor backButton = musicTable.getCells().get(musicTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) { @@ -171,7 +380,7 @@ public void clicked(InputEvent even, float x, float y) { private void handleBackButton() { createButton("Back",0, optionTable.getHeight()/5, optionTable); - Actor backButton = optionTable.getCells().get(2).getActor(); + Actor backButton = optionTable.getCells().get(optionTable.getCells().size - 1).getActor(); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent even, float x, float y) {