简体   繁体   English

如何在Screen libgdx中暂停游戏?

[英]How to pause the game in Screen libgdx?

Sorry for my question , but i am stuck. 对不起,我的问题,但我被困住了。 I am new in develop game with lib gdx and don`t judge me strictly. 我是lib gdx开发游戏的新手,请不要严格判断我。 I have my game activity: 我有游戏活动:

public class MyGame extends Game {
MenuScreen menu;
SplashScreen splash;
DefendScreen def;

@Override
public void create() {
    // Gdx.app.log("LogGame", "MyGame create");
    menu = new MenuScreen(this);
    splash = new SplashScreen(this);
    def = new DefendScreen(this);
    setScreen(splash);


}

@Override
public void resize(int width, int height) {

}

@Override
public void render() {
    super.render();
}

@Override
public void pause() {
    super.pause();

}

@Override
public void resume() {
    super.resume();

}

@Override
public void dispose() {

}

} }

I have two screens : 我有两个屏幕:

public class MenuScreen implements Screen, InputProcessor {
public MenuScreen(final MyGame gam) {
    game = gam;
    cam = new OrthographicCamera();
    cam.setToOrtho(false, 800, 480);
    w = Gdx.graphics.getWidth();
    h = Gdx.graphics.getHeight();
    defScreen = new DefendScreen(game);
    spriteBatch = new SpriteBatch();
    stage = new Stage();
}
@Override
public void dispose() {
    Gdx.app.log("LogGame", "splashs dispose");
    Gdx.input.setInputProcessor(null);

    try {

        spriteBatch.dispose();
        stage.dispose();
        font12.dispose();
    } catch (Exception e) {

    }

}

@Override
public void hide() {
    // TODO Auto-generated method stub

}
@Override
public void pause() {



}
@Override
public void render(float arg0) {
    SetCamera(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2f);
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(); // update all actors
    stage.draw();

}
@Override
public void resize(int arg0, int arg1) {
}

@Override
public void resume() {
    // TODO Auto-generated method stub

}
@Override
public void show() {
    stage.addActor(new WorldMenu(new Texture(Gdx.files
            .internal("images/bg/pause_back.png"))));

    Gdx.input.setInputProcessor(stage);
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(
            Gdx.files.internal("font/trebuchet_ms.ttf"));
    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.size = 25;
    font12 = generator.generateFont(parameter);
    generator.dispose();

    DrawLeftMeu();
    DrawRightMeu();
    DrawSetingsMenu();

}}

And game screen where i have pause button: 和游戏屏幕上我有暂停按钮:

public class DefendScreen implements Screen, InputProcessor {
public DefendScreen(final MyGame gam) {
    game = gam;
    cam = new OrthographicCamera();
    cam.setToOrtho(false, 800, 480);
    startTime = TimeUtils.millis();
    spriteBatch = new SpriteBatch();

    stage = new Stage();
    w = Gdx.graphics.getWidth();
    h = Gdx.graphics.getHeight();

}
@Override
public void dispose() {
    Gdx.app.log("LogGame", "defend dispose");
    try {
        combo0.dispose();
        combo1.dispose();
        combo2.dispose();
        good0.dispose();
        good1.dispose();
        bad0.dispose();
        bad1.dispose();
        bad2.dispose();
        bad3.dispose();
        bad4.dispose();
        music.dispose();
        music.stop();
        spriteBatch.dispose();
        font.dispose();
        pbar.dispose();
        scores.dispose();

    } catch (Exception e) {

    }

}
@Override
public void hide() {
}

@Override
public void pause() {

    // this.state = State.PAUSE;
}
@Override
public void render(float delta) {
    SetCamera(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2f);
    switch (state) {
    case RUN:
        RunGame();
        break;
    case PAUSE:
        // do stuff here

        break;
    case RESUME:

        break;

    default:
        break;
    }

}
@Override
public void resize(int arg0, int arg1) {
}

@Override
public void resume() {
    this.state = State.RESUME;
}

@Override
public void show() {

    regions = new ArrayList<Integer>();

    addRegions();
    initSounds();
    speedgame = speedgameEasy;

    stage.addActor(worldActor);
    stage.addActor(pauseActor);
    startTime = TimeUtils.millis();

    stage.addListener(clickList);

    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(
            Gdx.files.internal("font/trebuchet_ms.ttf"));
    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.size = 25;
    font = generator.generateFont(parameter);
    generator.dispose();

    pbar = new Texture(Gdx.files.internal("images/bg/line_indicator.png"));
    scores = new Texture(
            Gdx.files.internal("images/game_images/scorebox.png"));

    Gdx.input.setInputProcessor(stage);
    Gdx.input.setCatchBackKey(true);
}}

And my main questions !!! 和我的主要问题! --> what i must do that on pause action i can freeze the game action and call menu screen (whish have button resume)? ->在暂停动作上我必须怎么做才能冻结游戏动作并调用菜单屏幕(具有恢复按钮的功能)? --> what i mus do in resume onclick to show game action resume? ->我想在简历onclick中显示游戏动作简历吗? on stackoverflow an other forums i find some explains about my question, which means to implement in resume some switch case with choices of game state, but i am do not understand how its realize in my code. 在stackoverflow的另一个论坛上,我发现了一些有关我的问题的解释,这意味着可以通过选择游戏状态来实现一些切换情况,但是我不理解其在代码中的实现方式。 Thanks everyone. 感谢大家。

In your game screen you should have a button (an actor added to the stage) which implements a ClickListener that calls either pause() or resume() depending on the current state of the game. 在游戏屏幕上,您应该有一个按钮(在舞台上添加了一个演员),该按钮实现了一个ClickListener,它会根据游戏的当前状态来调用pause()resume()

It should look something like this: 它看起来应该像这样:

pauseButton.addListener(new ClickListener() {
        public void clicked(InputEvent evt, float x, float y) {
            if (!paused) Gdx.app.getApplicationListener().pause();
            else Gdx.app.getApplicationListener().resume();
        }
    });

Where paused is a boolean variable in your game screen class. paused是游戏屏幕类中的布尔变量。

In my opinion this is the easiest way to do this. 我认为这是最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM