我不能在LibGDX中暂停我的游戏

时间:2016-10-30 14:47:12

标签: libgdx pause

我试图通过触摸按钮暂停我的游戏。但我不能。

暂停游戏并继续游戏的最简单方法是什么?

以下是我的整个代码。我知道这很乱,但我真的很新。

public class PlayScreen implements Screen{

    FlapAndFly faf = new FlapAndFly();
    SpriteBatch sb;
    Random rnd;
    Ucak player;
    BigBoss boss;
    float ani =1f;
    Vector2 vec;
    Rectangle ucakRect,upRect,downRect,mermiRect,fireRect,rightRect,leftRect,bossRect,bossMermiRect,tavanRect,tabanRect;
    Vector2 yercekimi  = new Vector2();
    Vector2 pos = new Vector2();
    Vector3 touchPoint;
    OrthographicCamera cam;
    float x = 0;
    float boy;
    TextureRegion arka, arka1,arka2,arka3,arka4;
    Kayalar[] altkayalar = new Kayalar[50];
    Enemy[] enemy = new Enemy[50];

    Kayalar[] ustKayalar = new Kayalar[50];
    Rectangle[] altRect  = new Rectangle[50];
    Rectangle[] ustRect  = new Rectangle[50];
    Rectangle[] enemyRect  = new Rectangle[50];

    Mermi tekMermi,bossMermi;
    Animation bomba;
    int skor = 0;
    Vector2 mermiVec;
    Vector2 bossMermiVec;
    Vector2 enemyVec;
    Vector2 bossVec;
    boolean dus=false;
    boolean playerDus = false;
    ShapeRenderer sr;
    boolean ates = false;
    boolean bossAtes = false;
    boolean doldur = true;
    float s= 0;
    boolean bom = false;
    public int a;
    boolean right = true, left = false, up = false, down = false, savas = false;
    boolean ucakBom = false;
    float playerGuc = 1;
    float bossGuc = Gdx.graphics.getWidth()/6;
    Music play = Gdx.audio.newMusic(Gdx.files.internal("play.mp3"));


    Rectangle lazerRect,lazerbuttonRect,pauseRect;
    Lazer lazer;
    Vector2 lazerVec;
    boolean lazerAtes = false;
    Sound lSound = Gdx.audio.newSound(Gdx.files.internal("lazer.wav"));
    Sound gunSound = Gdx.audio.newSound(Gdx.files.internal("gun.wav"));
    Sound playerSound = Gdx.audio.newSound(Gdx.files.internal("heli.wav"));
    Sound bossSound = Gdx.audio.newSound(Gdx.files.internal("boss.wav"));
    boolean pause = false;


    public PlayScreen(FlapAndFly faf) {
        super();
        this.faf = faf;

    }

    @Override
    public void show() {
        Dosyalar.yükle();
        play.setVolume(0.5f);
        play.play();
        playerSound.loop();

        sb = new SpriteBatch();
        sr = new ShapeRenderer();
        rnd = new Random();
        bomba = new Animation(0.1f, Dosyalar.boomReg1,Dosyalar.boomReg2,Dosyalar.boomReg3,Dosyalar.boomReg4,Dosyalar.boomReg5);
        bomba.setPlayMode(Animation.PlayMode.LOOP);
        pos.x = 0;
        pos.y = 0;
        boy = 0;
        enemyVec = new Vector2(pos.x + Gdx.graphics.getWidth()/2 ,boy);
        player = new Ucak();
        player.ucakOlustur(0.03f, Dosyalar.ucak1Reg, Dosyalar.ucak2Reg, Dosyalar.ucak3Reg);

        for(int i = 0; i<altkayalar.length; i++){

            boy = rnd.nextInt((Gdx.graphics.getHeight()-Gdx.graphics.getHeight()/3));
            enemyVec.y = rnd.nextInt(Gdx.graphics.getHeight()-Gdx.graphics.getHeight()/2) + Gdx.graphics.getHeight()/4;
            ustKayalar[i] = new Kayalar(Dosyalar.kayaUstReg, pos.x+Gdx.graphics.getWidth()/2,
                        Gdx.graphics.getHeight()-boy,  Gdx.graphics.getWidth()/11, boy);

            altkayalar[i] = new Kayalar(Dosyalar.kayaReg, pos.x+Gdx.graphics.getWidth(), pos.y, Gdx.graphics.getWidth()/11, boy);
            altRect[i] = new Rectangle(altkayalar[i].x + altkayalar[i].width /2 , altkayalar[i].y , altkayalar[i].width/18, altkayalar[i].height);
            ustRect[i] = new Rectangle(ustKayalar[i].x + ustKayalar[i].width/2, ustKayalar[i].y, ustKayalar[i].width/18, ustKayalar[i].height);
            enemy[i] = new Enemy(Dosyalar.enemyReg, enemyVec.x, enemyVec.y,Gdx.graphics.getWidth()/15, Gdx.graphics.getHeight()/10);        
            pos.x+=(Gdx.graphics.getWidth());
            enemyVec.x+=(float) rnd.nextInt((int)(Gdx.graphics.getWidth())+Gdx.graphics.getWidth()/3);

        }

        vec = new Vector2(Gdx.graphics.getWidth()/30, Gdx.graphics.getHeight()/2-(Dosyalar.ucak1Reg.getRegionHeight()/2));
        yercekimi.set(Gdx.graphics.getWidth()/250, 0);
        cam = new OrthographicCamera();
        cam.setToOrtho(false, Gdx.graphics.getWidth(),Gdx.graphics.getHeight());    
        mermiVec = new Vector2();


        bossVec = new Vector2(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()*3/2);
        boss = new BigBoss(Dosyalar.heliReg1,Dosyalar.heliReg2,Gdx.graphics.getWidth()/6,
                Gdx.graphics.getHeight()/3,Gdx.graphics.getWidth()*2,Gdx.graphics.getHeight(), 0.01f);

        bossMermiVec = new Vector2();
        boss.ucakOlustur();

        lazerVec = new Vector2(cam.position.x - Gdx.graphics.getWidth(), cam.position.y- Gdx.graphics.getHeight()/2);
    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClearColor(0, 1, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


        sb.setProjectionMatrix(cam.combined);

        sb.begin();
        sb.draw(Dosyalar.arkaPlanReg, x - Gdx.graphics.getWidth(), 0,(float) (Gdx.graphics.getWidth()), (float)(Gdx.graphics.getHeight()));
        sb.draw(Dosyalar.arkaPlanReg1, x, 0,(float) (Gdx.graphics.getWidth()), (float)(Gdx.graphics.getHeight()));
        sb.draw(Dosyalar.arkaPlanReg2, x+Gdx.graphics.getWidth(), 0,(float) (Gdx.graphics.getWidth()), (float)(Gdx.graphics.getHeight()));
        sb.draw(Dosyalar.arkaPlanReg3, x+(Gdx.graphics.getWidth()*2), 0,(float) (Gdx.graphics.getWidth()), (float)(Gdx.graphics.getHeight()));
        sb.draw(Dosyalar.arkaPlanReg4, x+(Gdx.graphics.getWidth()*3), 0,(float) (Gdx.graphics.getWidth()), (float)(Gdx.graphics.getHeight()));
        sb.end();

                for(int i = 0; i < altkayalar.length; i++){

                    enemyRect[i] = new Rectangle(enemy[i].x, enemy[i].y,enemy[i].width,enemy[i].height);    

                    altkayalar[i].create(sb);
                    ustKayalar[i].create(sb);
                    altRect[i] = new Rectangle(altkayalar[i].x + altkayalar[i].width /2 , altkayalar[i].y , altkayalar[i].width/18, altkayalar[i].height);
                    ustRect[i] = new Rectangle(ustKayalar[i].x + ustKayalar[i].width/2, ustKayalar[i].y, ustKayalar[i].width/18, ustKayalar[i].height);
                }


        sb.begin();
        sb.draw(Dosyalar.ustReg, x-(Gdx.graphics.getWidth()), Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()/5),Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);
        sb.draw(Dosyalar.ustReg1, x, Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()/5),Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);
        sb.draw(Dosyalar.ustReg2, x+(Gdx.graphics.getWidth()), Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()/5),Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);
        sb.draw(Dosyalar.ustReg3, x+(Gdx.graphics.getWidth()*2), Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()/5),Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);        
        sb.draw(Dosyalar.ustReg4, x+(Gdx.graphics.getWidth()*3), Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()/5),Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);

        sb.draw(Dosyalar.altReg, x -Gdx.graphics.getWidth(), 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);
        sb.draw(Dosyalar.altReg1, x, 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);
        sb.draw(Dosyalar.altReg2, x+(Gdx.graphics.getWidth()), 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);
        sb.draw(Dosyalar.altReg3, x+(Gdx.graphics.getWidth()*2), 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);
        sb.draw(Dosyalar.altReg4, x+(Gdx.graphics.getWidth()*3), 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/5);

        sb.end();

        player.ucakCiz(sb, player.ucak.getKeyFrame(ani), vec.x, vec.y, Gdx.graphics.getWidth()/15, Gdx.graphics.getHeight()/10);

        for(int i = 0; i < altkayalar.length; i++){

            enemy[i].create(sb);
            enemy[i].gel(Gdx.graphics.getWidth()/250);

            }

        sb.begin();

        sb.draw(Dosyalar.controlReg, cam.position.x - (float)(Gdx.graphics.getWidth()/2),Gdx.graphics.getHeight()/15,(Gdx.graphics.getWidth()/6), (Gdx.graphics.getHeight()/4));        
        sb.draw(Dosyalar.fireReg,cam.position.x + (Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/7),Gdx.graphics.getHeight()/15,(Gdx.graphics.getWidth()/11), (Gdx.graphics.getHeight()/6));
        sb.draw(Dosyalar.ekranReg, cam.position.x - Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()-Gdx.graphics.getHeight()/8,Gdx.graphics.getWidth()/4,Gdx.graphics.getHeight()/8);
        sb.draw(Dosyalar.lazerReg,cam.position.x + (Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/4),Gdx.graphics.getHeight()/15,(Gdx.graphics.getWidth()/11), (Gdx.graphics.getHeight()/6));
        sb.draw(Dosyalar.pauseReg, cam.position.x  + Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/16 , cam.position.y + Gdx.graphics.getHeight()/2 - Gdx.graphics.getHeight()/9,(Gdx.graphics.getWidth()/16), Gdx.graphics.getHeight()/9);
        sb.end();

        pauseRect = new Rectangle(cam.position.x  + Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/16 , cam.position.y + Gdx.graphics.getHeight()/2 - Gdx.graphics.getHeight()/9,(Gdx.graphics.getWidth()/16), Gdx.graphics.getHeight()/9);
        ucakRect = new Rectangle(vec.x+player.width/4, vec.y,player.width/3*2, player.height);
        bossRect = new Rectangle(boss.x, boss.y, boss.width, boss.height);

        upRect = new Rectangle(cam.position.x - (Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/20),Gdx.graphics.getHeight()/5,(Gdx.graphics.getWidth()/15), (Gdx.graphics.getHeight()/7));
        downRect = new Rectangle(cam.position.x - (Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/20),Gdx.graphics.getHeight()/20,(Gdx.graphics.getWidth()/15), (Gdx.graphics.getHeight()/7)); 
        rightRect = new Rectangle(cam.position.x - (Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/10 ),Gdx.graphics.getHeight()/8,(Gdx.graphics.getWidth()/15), (Gdx.graphics.getHeight()/7));
        leftRect = new Rectangle (((cam.position.x -Gdx.graphics.getWidth()/2)),Gdx.graphics.getHeight()/8,(Gdx.graphics.getWidth()/15), (Gdx.graphics.getHeight()/7));

        tekMermi = new Mermi(mermiVec.x, ucakRect.y, ucakRect.width/8*3, ucakRect.height/8*3, Dosyalar.bulletReg);  
        bossMermi = new Mermi(bossMermiVec.x + ucakRect.width/8*3 , bossRect.y + ucakRect.height/8*3, ucakRect.width/8*3, ucakRect.height/8*3, Dosyalar.bossBulletReg);

        mermiRect = new Rectangle(tekMermi.x,tekMermi.y,tekMermi.width,tekMermi.height);
        fireRect = new Rectangle(cam.position.x + (Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/7),Gdx.graphics.getHeight()/15,(Gdx.graphics.getWidth()/11), (Gdx.graphics.getHeight()/6));

        bossMermiRect = new Rectangle(bossMermi.x,bossMermi.y,bossMermi.width,bossMermi.height);
        tavanRect = new Rectangle(cam.position.x -Gdx.graphics.getWidth()/2, cam.position.y + Gdx.graphics.getHeight()/2- Gdx.graphics.getHeight()/7 , Gdx.graphics.getWidth(),Gdx.graphics.getHeight()/7);
        tabanRect = new Rectangle(cam.position.x -Gdx.graphics.getWidth()/2, cam.position.y - Gdx.graphics.getHeight()/2, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/7);

        lazer = new Lazer(sr, lazerVec.x, lazerVec.y, Gdx.graphics.getWidth() / 45, Gdx.graphics.getHeight());
        lazerRect = new Rectangle(lazer.x,lazer.y, lazer.width,lazer.height);
        lazerbuttonRect = new Rectangle(cam.position.x + (Gdx.graphics.getWidth()/2 - Gdx.graphics.getWidth()/4),Gdx.graphics.getHeight()/15,(Gdx.graphics.getWidth()/11), (Gdx.graphics.getHeight()/6));


        tekMermi.render(sb);
        bossMermi.render(sb);
        boss.ucakCiz(sb);

        vec.add(yercekimi); 


        for(int z = 0; z<2; z++){

            if (Gdx.input.isTouched(z)) {

            touchPoint = new Vector3(Gdx.input.getX(z), Gdx.input.getY(z),0);
            cam.unproject(touchPoint);
            }
        }



        if(Gdx.input.justTouched()){

            if(pauseRect.contains(touchPoint.x,touchPoint.y)){

                pause = true;
            }

            if(fireRect.contains(touchPoint.x,touchPoint.y)){

                ates = true;
                doldur = false; 

            }

            if(lazerbuttonRect.contains(touchPoint.x,touchPoint.y)){

                if( playerGuc > 1){     

                    System.out.println("lazsrerr");
                    lazerVec.x = cam.position.x - Gdx.graphics.getWidth()/2 - lazer.width*3;
                    playerGuc -=5;
                    lazerAtes = true;

                        }
                else{



                    }
            }
        }

        if(pause){

            Gdx.graphics.setContinuousRendering(false);

            }

        if(!pause){

            Gdx.graphics.setContinuousRendering(true);
        }

        if(lazerVec.x > cam.position.x + Gdx.graphics.getWidth()/2){

            lazerAtes = false;
            lazerVec.x = cam.position.x - Gdx.graphics.getWidth();

        }

        if(lazerAtes){

            lSound.play();
            lazerVec.add(Gdx.graphics.getWidth()/15, 0);

        }

        for(int i = 0; i < enemy.length; i++){

            if(lazerRect.overlaps(enemyRect[i]))

            enemy[i].y = -200;

        }

        if(lazerRect.overlaps(bossRect)){

            bossGuc = bossGuc-5;
            System.out.println(bossGuc);
        }


        if(ates){

            mermiVec.x+=Gdx.graphics.getWidth()/20;

        } 

        if(mermiVec.x >= cam.position.x+Gdx.graphics.getWidth()/2+tekMermi.width){

            doldur = true;
            ates = false;
        }

        if(doldur){

            mermiVec.x = ucakRect.x+ucakRect.width/8;

        }

        if(dus){

            enemy[a].y -=15;
            enemy[a].x +=5;

        }

        for( int i = 0; i < enemyRect.length; i++){

            if(mermiRect.overlaps(enemyRect[i])){

                    skor+=1;
                    playerGuc +=Gdx.graphics.getWidth()/800;
                    System.out.println("playerGuc :" +playerGuc);
                    enemyRect[i].width = 0;
                    enemyRect[i].height = 0;
                    enemyRect[i].y = 0;
                    doldur = true;
                    ates= false;
                    bom = true;
                    dus = true;
                    a = i;

                }
        }

        if(bom){

            sb.begin();

            sb.draw(bomba.getKeyFrame(s), enemy[a].x, enemy[a].y, Gdx.graphics.getWidth()/15, Gdx.graphics.getHeight()/10);

            sb.end();
            s+=Gdx.graphics.getDeltaTime();

            if(s > 2){

                bom = false;
                s = 0;
            }
        }

            cam.position.x += yercekimi.x;

        if(Gdx.input.isTouched()){

            if(upRect.contains(touchPoint.x,touchPoint.y)){

                vec.y+=Gdx.graphics.getWidth()/160;
                if(vec.y >= Gdx.graphics.getHeight() -  ucakRect.height){
                    vec.y = Gdx.graphics.getHeight() -  ucakRect.height;
                }
            }


            if(downRect.contains(touchPoint.x,touchPoint.y)){
                vec.y -=Gdx.graphics.getWidth()/160;

                if(vec.y <= 0){
                    vec.y=0;
                }

            }
            if(rightRect.contains(touchPoint.x,touchPoint.y)){

                vec.x+=Gdx.graphics.getWidth()/220;

                if(vec.x>cam.position.x + Gdx.graphics.getWidth()/2-ucakRect.width){

                    vec.x = cam.position.x + Gdx.graphics.getWidth()/2-ucakRect.width;
                }
            }
            if(leftRect.contains(touchPoint.x,touchPoint.y)){

                vec.x-=Gdx.graphics.getWidth()/220;

                if(vec.x < cam.position.x -Gdx.graphics.getWidth()/2){

                    vec.x = cam.position.x -Gdx.graphics.getWidth()/2;
                }
            }
        }

        cam.update();


        if(vec.x >= x+(Gdx.graphics.getWidth()+Gdx.graphics.getWidth()/2)){

            x+=Gdx.graphics.getWidth();

        }

        if(mermiRect.overlaps(bossRect)){

            bossGuc -=playerGuc/5;

            System.out.println("bossGuc : " +bossGuc);
        }

        if(ucakBom){

            sb.begin();

            sb.draw(bomba.getKeyFrame(s), ucakRect.x, ucakRect.y, Gdx.graphics.getWidth()/15, Gdx.graphics.getHeight()/10);

            sb.end();

            s+=Gdx.graphics.getDeltaTime();
            System.out.println(s);

            if(s > 2){

                ucakBom = false;
                s = 0;

                }
            }

        if(playerDus){

            vec.y-=5;

            if(vec.y<0){
                play.stop();
                playerSound.stop();
                bossSound.stop();
                faf.setScreen(new GameOver(faf));   
            }
        }


        for(int j = 0; j < altkayalar.length; j++){

            if(ucakRect.overlaps(altRect[j]) || ucakRect.overlaps(enemyRect[j]) || ucakRect.overlaps(ustRect[j])){

                play.stop();
                playerSound.stop();
                bossSound.stop();
                faf.setScreen(new GameOver(faf));

            }
        }

        if(ucakRect.overlaps(bossRect) || ucakRect.overlaps(bossMermiRect) || ucakRect.overlaps(tavanRect) || ucakRect.overlaps(tabanRect)){

        ucakBom = true;
        playerDus = true;

    }

        if( boss.x - vec.x < Gdx.graphics.getWidth()-199){

            yercekimi.set(0,0);
            savas = true;
        }

        if(savas){
        Savas();
        }

        if(bossGuc < 0){

            play.stop();
            playerSound.stop();
            bossSound.stop();
            faf.setScreen(new Clear1(faf));

        }
        ani+=delta;

        sr.setProjectionMatrix(cam.combined);

        sr.begin(ShapeType.Filled);
        sr.setColor(Color.BLUE);
        sr.rect(cam.position.x - Gdx.graphics.getWidth()/2 + Gdx.graphics.getWidth()/12, Gdx.graphics.getHeight()- Gdx.graphics.getHeight()/17 , playerGuc,  Gdx.graphics.getHeight()/22);
        sr.setColor(Color.RED);
        sr.rect(cam.position.x - Gdx.graphics.getWidth()/2 +  Gdx.graphics.getWidth()/12, Gdx.graphics.getHeight() -  Gdx.graphics.getHeight()/8, bossGuc, Gdx.graphics.getHeight()/22);
        sr.end();


    }

    public void PauseGame(){        



    }

    public void Savas(){

//      bossSound.loop();

        if(boss.y > Gdx.graphics.getHeight()-boss.height){

                up = false;
                down = true;    
            }

        if(boss.y < 0){

                down = false;   
                up = true;
            }

        if(boss.x < cam.position.x - Gdx.graphics.getWidth()/3){

            right = true;
            left = false;

        }

        if(boss.x > cam.position.x + Gdx.graphics.getWidth()/2 - boss.width){

            left = true;
            right = false;

        }

        if(down){

            boss.Down();
        }

        if(up){

            boss.Up();

        }

        if(right){

            boss.Right();
        }

        if(left){

            boss.Left();
        }

        if(bossRect.y %93 == 0){

            bossAtes = true;

        }

        if(bossAtes){

            bossMermiVec.x-=20;
        }

        if(!bossAtes){

            bossMermiVec.x = bossRect.x;
        }

        if(bossMermiVec.x < cam.position.x -Gdx.graphics.getWidth() -100){

            bossAtes = false; 
            }

        }
    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub


    }

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

    }

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

    }

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

    }

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

    }

}

3 个答案:

答案 0 :(得分:0)

就像你有一个PlayScreen一样,你也可以制作一个PauseScreen。只需将您要保存的所有信息传递到PauseScreen(播放器位置,分数等)。当您返回PlayScreen时,只需将所有原始数据返回。

另一种方法是停止所有更新方法,例如render(),只保留完全相同的PlayScreen实例,以便重复使用它。

答案 1 :(得分:0)

我还是比较新的(整体编码一年),但我正在使用以下内容进行我正在开发的游戏...... 根据需要修改代码并根据需要进行优化。我切断了很多代码,以便更容易理解。

public enum GAMESTATE{
    RUNNING, PAUSED, FAILED, PASSED;
}




    @Override
public void show() {
    camera = new OrthographicCamera();
    camera.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2, 0);
    camera.update();
    viewport = new StretchViewport(WORLD_WIDTH, WORLD_HEIGHT, camera);

    // REST OF CODE

    gamestate = GAMESTATE.RUNNING;

    // REST OF CODE
}

@Override
public void render(float delta) {
    switch (gamestate){

        case RUNNING:
            update(delta);
            renderRunning(delta);
            break;

        case PAUSED:
            renderPaused(delta);
            break;

        case FAILED:
            levelFailed();
            break;

        case PASSED:
            levelPassed();
            break;
    }
}




@Override
public void pause() {
    gamestate = GAMESTATE.PAUSED;
}

@Override
public void resume() {
    gamestate = GAMESTATE.PAUSED;
}

@Override
public void hide() {
    gamestate = GAMESTATE.PAUSED;
}




 private class PauseButton extends Actor {

    private Texture pause;

    private PauseButton(){
        pause = new Texture("button/pause.png");

        setWidth(150);
        setHeight(150);
        setBounds(1200, 2100, getWidth(), getHeight());
        setTouchable(Touchable.enabled);
        Gdx.input.setInputProcessor(buttonStage);
        addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                gamestate = GAMESTATE.PAUSED;
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            }
        });
    }


    @Override
    public void draw(Batch batch, float alpha){
        batch.setProjectionMatrix(GameScreen.camera.combined);
        batch.draw(pause, 1200, 2100);
    }

    @Override
        public void act(float delta){
            Gdx.input.setInputProcessor(buttonStage);
    }
}






private class ResumeButton extends Actor {

    private Texture resume;

    private ResumeButton(){
        resume = new Texture("button/resume.png");

        setWidth(150);
        setHeight(150);
        setBounds(1200, 2100, getWidth(), getHeight());
        setTouchable(Touchable.enabled);
        Gdx.input.setInputProcessor(resumeButtonStage);
        addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                gamestate = GAMESTATE.RUNNING;
                PauseMenuActor.timer = 0;
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            }
        });
    }


    @Override
    public void draw(Batch batch, float alpha){
        batch.setProjectionMatrix(GameScreen.camera.combined);
        if (gamestate == GAMESTATE.PAUSED) {
            batch.draw(resume, 1200, 2100);
        }
    }

    @Override
    public void act(float delta){
        Gdx.input.setInputProcessor(resumeButtonStage);
    }
}

使用Stage(libgdx API)添加按钮actor

Stage stage, resumeButtonStage;

private void defineButtonStage(){
    // Pause Button Stage
    buttonStage = new Stage(new ScreenViewport());
    buttonStage.setViewport(viewport);

    PauseButtonSprite pauseButtonSprite = new PauseButtonSprite();
    buttonStage.addActor(pauseButtonSprite);


    // Resume Button Stage.
    resumeButtonStage = new Stage(new ScreenViewport());
    resumeButtonStage.setViewport(viewport);

    ResumeButtonSprite resumeButtonSprite = new ResumeButtonSprite();
    resumeButtonStage.addActor(resumeButtonSprite);

}

答案 2 :(得分:-1)

处理此问题的一种简单方法是创建一个处理所有不同状态的GameStateManager类,它可以让您从PlayState(屏幕)切换到PauseState。

GameState设计信息:http://blog.nuclex-games.com/tutorials/cxx/game-state-management/