java - 如何在 libGDX 游戏项目中避免 "static"

标签 java oop static libgdx

我目前正在重新设计我的代码,因为它很难阅读而且不是真正的 OOP。 我正在开发 libGDX 游戏,所以代码是用 Java 编写的。 目前我正在为一些静态方法和变量而苦苦挣扎。我想避开它们,但我不知道该怎么做。 在我的游戏项目中,我使用了几个不同的类。有实际的游戏屏幕、一个暂停屏幕和一个创建类,它在我第一次打开屏幕时创建 Assets 。 游戏屏幕中有一个 int,我用它来检测游戏当前是在运行还是暂停。但我不知道如何在不将其设为静态的情况下更改此变量。

感谢您的每一个回答:)! 干杯,乔什弗拉克斯

编辑:这是 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();
}
}

最佳答案

您可以在Create 类中创建GameScreen 对象,并将Create 类的句柄传递给它。这样你就可以在 GameScreen 中使用 Create 的对象,反之亦然。我无法测试,只能编译它。但这肯定对您有帮助。

这是我的代码。注意 - 我已经删除了注释代码并重新格式化了一些以便更清晰。

Here's Create class

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);
    }
}

And here's GameScreen class

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 上工作过,至少现在我能够感受到它。

关于java - 如何在 libGDX 游戏项目中避免 "static",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31626602/

相关文章:

java - 如何使用spring websocket向自定义用户发送自定义消息?

java - 云端点 : Arrays or collections of entity types are not allowed

java - 将对象传递给类时,如何访问它的变量/函数?

Ruby:从父类访问调用子类常量?

matlab - 验证属性是 MATLAB 中抽象类的子类

java - 解析进程 : keeping classes/instances separate or having them chained?

C++ 静态虚拟成员?

c++ - 如何重置函数内的静态 vector ?

c++ - C++ lambda 和静态变量的预期行为

java - 在 java 中跨平台地在没有时间的情况下播种随机生成器