问题描述
我目前正在重新设计我的代码,因为它难以阅读,而不是真正的OOP。 我正在开发一个libGDX游戏,因此代码是用Java编写的。 目前,我正在使用一些静态方法和变量。 我想避免它们,但我不知道如何。 在我的游戏项目中,我使用了几个不同的类。 有实际的游戏屏幕,暂停屏幕和创建类,当我第一次打开屏幕时会创建资产。 游戏画面中有一个int用于检测游戏当前是在运行还是暂停。 但我不知道如何在不使其变为静态的情况下更改此变量。
谢谢你的每一个答案:)! 干杯,Joshflux
编辑:这是GameScreen和Create类的代码。 这可能有点令人困惑:/。
public class GameScreen implements Screen{
private Texture[] monsterTextures = {Assets.manager.get(("Ressources/DemonHunter.jpg"), Texture.class), Assets.manager.get(("Ressources/WingedDemon.jpg"), Texture.class),
Assets.manager.get(("Ressources/Viking.jpg"), Texture.class), Assets.manager.get(("Ressources/DemonWarrior.jpg"), Texture.class)};
private Image[] monsterImages = {new Image(monsterTextures[0]), new Image(monsterTextures[1]), new Image(monsterTextures[2]), new Image(monsterTextures[3])};
private Stage gameStage = new Stage();// pauseStage = new Stage(), skillStage = new Stage();
private Table table = new Table();//, skillTable = new Table();
private Skin menuSkin = Assets.menuSkin;
/*private TextButton buttonContinue = new TextButton("Continue", menuSkin),
buttonExit = new TextButton("Exit", menuSkin),
buttonSkill = new TextButton("Skills", menuSkin),
buttonBack = new TextButton("Back", menuSkin),
buttonStrength = new TextButton("Strength", menuSkin),
buttonCrit = new TextButton("Crit", menuSkin),
buttonExp = new TextButton("Exp", menuSkin);
private Label pauseLabel = new Label ("Pause", menuSkin), skillLabel = new Label ("Skills", menuSkin);*/
private int randomMonster;
private int currentMonsterLife = 1 + (int)(Math.random() * ((10-1) + 1));
private TextField hitpoints = new TextField("Hitpoints: " + currentMonsterLife, menuSkin);
public int exp = 0, lvl = 1, skillpoints = 0;
public TextField stats = new TextField(" " + "Level: " + lvl + ", Exp: " + exp + ", Skillpoints: " + skillpoints, menuSkin);
//public static final int GAME_CREATING = 0;
public final int GAME_RUNNING = 1;
public final int GAME_PAUSED = 2;
public final int GAME_SKILLED = 3;
private int gamestatus;
@Override
public void show() {
randomMonster = 0 + (int)(Math.random() * ((3-0) + 1));
gameStage.addActor(monsterImages[randomMonster]);
Gdx.input.setInputProcessor(gameStage);
}
public void newMonster() {
exp += 1;
if(exp >= lvl * 5 * 2) {lvl += 1; skillpoints += 1;}
monsterImages[randomMonster].remove();
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
randomMonster = 0 + (int)(Math.random() * ((3-0) + 1));
currentMonsterLife = 1 + (int)(Math.random() * ((5-1) + 1));
hitpoints = new TextField("Hitpoints: " + currentMonsterLife, menuSkin);
table.removeActor(stats);
stats = new TextField(" " + "Level: " + lvl + ", Exp: " + exp + ", Skillpoints: " + skillpoints, menuSkin);
table.add(stats).width(250).row();
gameStage.addActor(monsterImages[randomMonster]);
}
@Override
public void render(float delta) {
if(Gdx.input.isKeyJustPressed(Keys.ESCAPE)) pauseGame();
if(gamestatus == GAME_PAUSED)Create.screenPause(); // Not working
/*if(gamestatus == GAME_CREATING) {
buttonContinue.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gamestatus = GAME_RUNNING;
}
});
buttonExit.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gamestatus = GAME_PAUSED;
}
});
buttonSkill.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gamestatus = GAME_SKILLED;
}
});
buttonBack.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gamestatus = GAME_RUNNING;
}
});
table.add(pauseLabel).padBottom(40).row();
table.add(buttonContinue).size(150, 60).padBottom(20).row();
table.add(buttonExit).size(150, 60).padBottom(20).row();
table.add(buttonSkill).size(150, 60).padBottom(20).row();
table.add(stats).width(250).padBottom(10).row();
table.setFillParent(true);
pauseStage.addActor(table);
skillTable.add(pauseLabel).padBottom(40).row();
skillTable.add(buttonStrength).size(150, 60).padBottom(20).row();
skillTable.add(buttonCrit).size(150, 60).padBottom(20).row();
skillTable.add(buttonExp).size(150, 60).padBottom(20).row();
skillTable.add(buttonBack).size(150, 60).padBottom(20).row();
skillTable.setFillParent(true);
skillStage.addActor(skillTable);
gamestatus = GAME_RUNNING;
}*/
if(gamestatus == GAME_RUNNING) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.addActor(hitpoints);
gameStage.act();
gameStage.draw();
if(Gdx.input.justTouched()) {
currentMonsterLife -= 1;
hitpoints = new TextField("Hitpoints: " + currentMonsterLife, menuSkin);
}
if(currentMonsterLife == 0) {
newMonster();
}
}
/*if(gamestatus == GAME_PAUSED) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
pauseStage.act();
pauseStage.draw();
}*/
/*if(gamestatus == GAME_SKILLED) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
skillStage.act();
skillStage.draw();
}*/
}
public void pauseGame() {
gamestatus = GAME_PAUSED;
}
public void runGame() {
gamestatus = GAME_RUNNING;
}
public void skillGame() {
gamestatus = GAME_SKILLED;
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void pause() {
pauseGame();
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
for(int i = 0; i < monsterTextures.length; i++) {
monsterTextures[i].dispose();
}
gameStage.dispose();
//pauseStage.dispose();
menuSkin.dispose();
}
}
public class Create {
private Skin menuSkin = Assets.menuSkin;
private Stage pauseStage = new Stage(), skillStage = new Stage();;
private Table pauseTable = new Table(), skillTable = new Table();;
private TextButton pauseContinue = new TextButton("Continue", menuSkin),
pauseExit = new TextButton("Exit", menuSkin),
pauseSkill = new TextButton("Skills", menuSkin),
buttonStrength = new TextButton("Strength", menuSkin),
buttonCrit = new TextButton("Crit", menuSkin),
buttonExp = new TextButton("Exp", menuSkin),
buttonBack = new TextButton("Back", menuSkin);
private Label pauseLabel = new Label ("Pause", menuSkin), skillLabel = new Label ("Skills", menuSkin);
public void screenGame(){
pauseContinue.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Client.aGameScreen.runGame(); //Not working at the moment
}
});
pauseExit.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
pauseSkill.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
GameScreen.skillGame();
}
});
pauseTable.add(pauseLabel).padBottom(40).row();
pauseTable.add(pauseContinue).size(150, 60).padBottom(20).row();
pauseTable.add(pauseExit).size(150, 60).padBottom(20).row();
pauseTable.add(pauseSkill).size(150, 60).padBottom(20).row();
pauseTable.add(GameScreen.stats).width(250).padBottom(10).row();
pauseTable.setFillParent(true);
pauseStage.addActor(pauseTable);
}
public void screenPause(){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
pauseStage.act();
pauseStage.draw();
}
public void screenSkill(){
skillTable.add(pauseLabel).padBottom(40).row();
skillTable.add(buttonStrength).size(150, 60).padBottom(20).row();
skillTable.add(buttonCrit).size(150, 60).padBottom(20).row();
skillTable.add(buttonExp).size(150, 60).padBottom(20).row();
skillTable.add(buttonBack).size(150, 60).padBottom(20).row();
skillTable.setFillParent(true);
skillStage.addActor(skillTable);
}
}
这是Assets类:
public class Assets {
public static AssetManager manager = new AssetManager();
public static Skin menuSkin;
public static void queueLoading() {
manager.load("Skins/uiskin.atlas", TextureAtlas.class);
manager.load("Ressources/DemonHunter.jpg", Texture.class);
manager.load("Ressources/DemonWarrior.jpg", Texture.class);
manager.load("Ressources/WingedDemon.jpg", Texture.class);
manager.load("Ressources/Viking.jpg", Texture.class);
}
public static void setMenuSkin() {
if (menuSkin == null)
menuSkin = new Skin(Gdx.files.internal("Skins/uiskin.json"),
manager.get("Skins/uiskin.atlas", TextureAtlas.class));
}
public static boolean update() {
return manager.update();
}
}
1楼
您可以在Create
类中创建GameScreen
对象并传递Create
类的句柄。
这样你可以在GameScreen
使用Create
对象,反之亦然。
我无法测试,只能编译它。
但这肯定会帮助你。
这是我的代码。 注意 - 我删除了注释代码并进行了一些重新格式化以便更清晰。
这是
Create
类
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
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;
public class Create {
private Skin menuSkin = Assets.menuSkin;
private Stage pauseStage = new Stage(), skillStage = new Stage();
private Table pauseTable = new Table(), skillTable = new Table();
private TextButton pauseContinue = new TextButton("Continue", menuSkin),
pauseExit = new TextButton("Exit", menuSkin),
pauseSkill = new TextButton("Skills", menuSkin),
buttonStrength = new TextButton("Strength", menuSkin),
buttonCrit = new TextButton("Crit", menuSkin),
buttonExp = new TextButton("Exp", menuSkin),
buttonBack = new TextButton("Back", menuSkin);
private Label pauseLabel = new Label("Pause", menuSkin);
private GameScreen gameScreen = new GameScreen(this);
public void screenGame() {
pauseContinue.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameScreen.runGame();
}
});
pauseExit.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
pauseSkill.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameScreen.skillGame();
}
});
pauseTable.add(pauseLabel).padBottom(40).row();
pauseTable.add(pauseContinue).size(150, 60).padBottom(20).row();
pauseTable.add(pauseExit).size(150, 60).padBottom(20).row();
pauseTable.add(pauseSkill).size(150, 60).padBottom(20).row();
pauseTable.add(gameScreen.stats).width(250).padBottom(10).row();
pauseTable.setFillParent(true);
pauseStage.addActor(pauseTable);
}
public void screenPause() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
pauseStage.act();
pauseStage.draw();
}
public void screenSkill() {
skillTable.add(pauseLabel).padBottom(40).row();
skillTable.add(buttonStrength).size(150, 60).padBottom(20).row();
skillTable.add(buttonCrit).size(150, 60).padBottom(20).row();
skillTable.add(buttonExp).size(150, 60).padBottom(20).row();
skillTable.add(buttonBack).size(150, 60).padBottom(20).row();
skillTable.setFillParent(true);
skillStage.addActor(skillTable);
}
}
这是
GameScreen
类
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
public class GameScreen implements Screen {
private Create create;
public GameScreen(Create create) {
this.create = create;
}
private Texture[] monsterTextures = {
Assets.manager.get(("Ressources/DemonHunter.jpg"), Texture.class),
Assets.manager.get(("Ressources/WingedDemon.jpg"), Texture.class),
Assets.manager.get(("Ressources/Viking.jpg"), Texture.class),
Assets.manager.get(("Ressources/DemonWarrior.jpg"), Texture.class) };
private Image[] monsterImages = { new Image(monsterTextures[0]),
new Image(monsterTextures[1]), new Image(monsterTextures[2]),
new Image(monsterTextures[3]) };
private Stage gameStage = new Stage();
private Table table = new Table();
private Skin menuSkin = Assets.menuSkin;
private int randomMonster;
private int currentMonsterLife = 1 + (int) (Math.random() * ((10 - 1) + 1));
private TextField hitpoints = new TextField("Hitpoints: "
+ currentMonsterLife, menuSkin);
public int exp = 0, lvl = 1, skillpoints = 0;
public TextField stats = new TextField(" " + "Level: " + lvl + ", Exp: "
+ exp + ", Skillpoints: " + skillpoints, menuSkin);
public final int GAME_RUNNING = 1;
public final int GAME_PAUSED = 2;
public final int GAME_SKILLED = 3;
private int gamestatus;
@Override
public void show() {
randomMonster = 0 + (int) (Math.random() * ((3 - 0) + 1));
gameStage.addActor(monsterImages[randomMonster]);
Gdx.input.setInputProcessor(gameStage);
}
public void newMonster() {
exp += 1;
if (exp >= lvl * 5 * 2) {
lvl += 1;
skillpoints += 1;
}
monsterImages[randomMonster].remove();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
randomMonster = 0 + (int) (Math.random() * ((3 - 0) + 1));
currentMonsterLife = 1 + (int) (Math.random() * ((5 - 1) + 1));
hitpoints = new TextField("Hitpoints: " + currentMonsterLife, menuSkin);
table.removeActor(stats);
stats = new TextField(" " + "Level: " + lvl + ", Exp: " + exp
+ ", Skillpoints: " + skillpoints, menuSkin);
table.add(stats).width(250).row();
gameStage.addActor(monsterImages[randomMonster]);
}
@Override
public void render(float delta) {
if (Gdx.input.isKeyJustPressed(Keys.ESCAPE))
pauseGame();
if (gamestatus == GAME_PAUSED)
create.screenPause(); // Not working
if (gamestatus == GAME_RUNNING) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.addActor(hitpoints);
gameStage.act();
gameStage.draw();
if (Gdx.input.justTouched()) {
currentMonsterLife -= 1;
hitpoints = new TextField("Hitpoints: " + currentMonsterLife,
menuSkin);
}
if (currentMonsterLife == 0) {
newMonster();
}
}
}
public void pauseGame() {
gamestatus = GAME_PAUSED;
}
public void runGame() {
gamestatus = GAME_RUNNING;
}
public void skillGame() {
gamestatus = GAME_SKILLED;
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void pause() {
pauseGame();
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
for (int i = 0; i < monsterTextures.length; i++) {
monsterTextures[i].dispose();
}
gameStage.dispose();
// pauseStage.dispose();
menuSkin.dispose();
}
}
PS - 我从未在Android
,至少现在我能够感受到它。