回到主屏幕

时间:2018-03-03 09:45:14

标签: java libgdx

我为按钮调整了图像,我的游戏有2个阶段,一个是主屏幕,另一个是游戏画面。我把两个按钮放到了游戏中,一个是homebutton,当我点击它时我想回到主屏幕阶段,但有一个问题我无法解决。如果我们开始玩游戏并立即死亡,游戏结束阶段就会到来,当我点击家庭按钮屏幕时,它运作良好(它指向家庭舞台)。但是如果我们开始玩并且至少玩5-10秒并且死亡,那么游戏结束阶段就会到来,当我点击家庭按钮屏幕时,会出现一个奇怪的问题。当我点击时,主屏幕出现但立即返回到gameover阶段。我的意思是它打开主屏幕阶段,但马上回到游戏画面。解决方案是什么?

//home screen button
    Texture homeButtonTexture = new Texture(Gdx.files.internal("homebutton5.png"));
    final Image homeButtonImage = new Image(homeButtonTexture);
    homeButtonImage.setPosition(4*Gdx.graphics.getWidth()/10,Gdx.graphics.getHeight()/4+Gdx.graphics.getHeight()/22);
    homeButtonImage.setSize(Gdx.graphics.getWidth()/12,Gdx.graphics.getHeight()/10);

    homeButtonImage.addListener(new ClickListener(){
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){

                    if(gameState == 2){

                        butonclick.play(0.6f);

                        gameState = 0;

                        birdY = 2 * Gdx.graphics.getHeight() / 3;   // Oyunu tekrar başlatırsak kuşun yerini ayarlıyor

                        Gdx.input.setInputProcessor(stagemain);

                        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
                        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                        stagemain.act(Gdx.graphics.getDeltaTime());

                        stagemain.getBatch().begin();                                                                                    // stagemain nin background u ayarlama
                        stagemain.getBatch().draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());               // stagemain nin background u ayarlama
                        stagemain.getBatch().draw(bird, birdX, birdY, Gdx.graphics.getWidth() / 15, Gdx.graphics.getHeight() / 10);
                        stagemain.getBatch().end();                                                                                     // stagemain nin background u ayarlama
                        stagemain.draw();

                    }

                    return true;

        }
    });
    stage.addActor(homeButtonImage);

这是homebutton的定义,onCreate part。

else if (gameState == 2) {    // Gameover stage




        Gdx.input.setInputProcessor(stage);


        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());

        stage.getBatch().begin();                                                                      // stage nin background u ayarlama
        stage.getBatch().draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());  // stage nin background u ayarlama
        fonth.draw(stage.getBatch(),"Highscore: "+String.valueOf(highscore),2*Gdx.graphics.getWidth()/7,2*Gdx.graphics.getHeight()/3);  // stage üzerine bitmapfont yazdırma
        font.draw(stage.getBatch(),"Current score: "+String.valueOf(score),2*Gdx.graphics.getWidth()/7,4*Gdx.graphics.getHeight()/8);
        font.setColor(Color.RED);
        stage.getBatch().end();                                                                        // stage nin background u ayarlama

        stage.draw();

这是gameover阶段的定义,如果gameState == 2,它指向gameover阶段,我把它放在渲染部分。

 if(gameState == 0){
        Gdx.input.setInputProcessor(stagemain);

        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stagemain.act(Gdx.graphics.getDeltaTime());

        stagemain.getBatch().begin();                                                                                    // stagemain nin background u ayarlama
        stagemain.getBatch().draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());               // stagemain nin background u ayarlama
        stagemain.getBatch().draw(bird, birdX, birdY, Gdx.graphics.getWidth()/15, Gdx.graphics.getHeight()/10);
        stagemain.getBatch().end();                                                                                     // stagemain nin background u ayarlama
        stagemain.draw();

    }

这是主页,如果gamestate == 0,它指向主页阶段,我把它放在渲染部分

 @Override
public void render() {

    batch.begin();

    // Drawing stage1, means home screen
    if(gameState == 0){
        Gdx.input.setInputProcessor(stagemain);

        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stagemain.act(Gdx.graphics.getDeltaTime());

        stagemain.getBatch().begin();                                                                                    // stagemain nin background u ayarlama
        stagemain.getBatch().draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());               // stagemain nin background u ayarlama
        stagemain.getBatch().draw(bird, birdX, birdY, Gdx.graphics.getWidth()/15, Gdx.graphics.getHeight()/10);
        stagemain.getBatch().end();                                                                                     // stagemain nin background u ayarlama
        stagemain.draw();

    }

    // -------------------

    batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    // Game playing
    if (gameState == 1) {


        if (xKonumları[scoredObstacle] < birdX) {         // Score tablosunu ayarlamak. Eğer kuş, x inci engel sütununu geçmişse skoru 1 artır diyoruz.
            score++;

            if (scoredObstacle < 3) {              //0,1,2,3 üncü setlerdeyken döngüyü devam ettirmesi için bunu yazdık aksi takdirde 4 veya 5 olursa xKonumları[5] gibi bir engel setimiz yok.
                scoredObstacle++;
            } else {
                scoredObstacle = 0;
            }
        }

        if (Gdx.input.justTouched()) {

            flapping.play(0.8f);   // ses kanat çırpma ve ses ayarlama

            velocity = -17;
        }

        for (int i = 0; i < 4; i++) {

            if (xKonumları[i] < -Gdx.graphics.getWidth() / 15) {

                xKonumları[i] = xKonumları[i] + 4 * distance;  // bu ne için ?

                engelBoyu1[i] = random.nextFloat() * Gdx.graphics.getHeight(); // boylarını random atamak istiyorum
                engelBoyu2[i] = random.nextFloat() * Gdx.graphics.getHeight(); // boylarını random atamak istiyorum

            } else {
                xKonumları[i] = xKonumları[i] - engelVelocity;
            }

            if ((engelBoyu2[i] > ekran / 16) & (engelBoyu1[i] > ekran / 16)) {
                if ((26 * Gdx.graphics.getHeight() / 200 + engelBoyu1[i]) + engelBoyu2[i] < ekran) {
                    if ((((ekran - (26 * Gdx.graphics.getHeight() / 200 + engelBoyu1[i])) - engelBoyu2[i]) > ekran / 6) & (((ekran - (26 * Gdx.graphics.getHeight() / 200 + engelBoyu1[i])) - engelBoyu2[i]) < ekran / 5)) {


                        batch.draw(obstacleup1, xKonumları[i], 26 * Gdx.graphics.getHeight() / 200, Gdx.graphics.getWidth() / 15, engelBoyu1[i]);
                        batch.draw(obstacledown1, xKonumları[i], Gdx.graphics.getHeight() - engelBoyu2[i], Gdx.graphics.getWidth() / 15, engelBoyu2[i]);


                        engelBoyu1Rectangle[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/27,   26*ekran/200,   Gdx.graphics.getWidth()/160,   engelBoyu1[i]); // ortadaki rectangle
                        engelBoyu1Rectangle2[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/29,   26*ekran/200,   Gdx.graphics.getWidth()/110,   3*engelBoyu1[i]/4); // ortanın bir solu rectangle
                        engelBoyu1Rectangle3[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/36,   26*ekran/200,   Gdx.graphics.getWidth()/48,   2*engelBoyu1[i]/4); // ortanın 2 solundaki rectangle
                        engelBoyu1Rectangle4[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/50,   26*ekran/200,   Gdx.graphics.getWidth()/30,   2*engelBoyu1[i]/5); // ortanın 3 solundaki rectangle
                        engelBoyu1Rectangle5[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/83,   26*ekran/200,   Gdx.graphics.getWidth()/20,   1*engelBoyu1[i]/4); // ortanın 4 solundaki rectangle

                        engelBoyu2Rectangle[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/27,   ekran-engelBoyu2[i],  Gdx.graphics.getWidth()/160,   engelBoyu2[i]); // ortadaki rectangle
                        engelBoyu2Rectangle2[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/29,   ekran-3*engelBoyu2[i]/4,   Gdx.graphics.getWidth()/110,  3*engelBoyu2[i]/4); // ortanın bir solu rectangle
                        engelBoyu2Rectangle3[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/36,   ekran-2*engelBoyu2[i]/4,   Gdx.graphics.getWidth()/48,   2*engelBoyu2[i]/4);  // ortanın 2 solundaki rectangle
                        engelBoyu2Rectangle4[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/50,   ekran-2*engelBoyu2[i]/5,   Gdx.graphics.getWidth()/30,   2*engelBoyu2[i]/5);  // ortanın 3 solundaki rectangle
                        engelBoyu2Rectangle5[i] = new Rectangle(xKonumları[i]+Gdx.graphics.getWidth()/83,   ekran-1*engelBoyu2[i]/4,   Gdx.graphics.getWidth()/20,   1*engelBoyu2[i]/4);  // ortanın 4 solundaki rectangle


                    } else {
                        engelBoyu1[i] = random.nextFloat() * Gdx.graphics.getHeight();
                        engelBoyu2[i] = random.nextFloat() * Gdx.graphics.getHeight();
                    }
                } else {
                    engelBoyu1[i] = random.nextFloat() * Gdx.graphics.getHeight();
                    engelBoyu2[i] = random.nextFloat() * Gdx.graphics.getHeight();
                }
            } else {
                engelBoyu1[i] = random.nextFloat() * Gdx.graphics.getHeight();
                engelBoyu2[i] = random.nextFloat() * Gdx.graphics.getHeight();
            }
        }

        if ( (birdY > ekran/10) && (birdY<ekran) ) {    // bird eğer tanımladığımız sınırın üstünde veya altındaysa hareketi başlatabiliriz
            velocity = velocity + gravity;
            birdY = birdY - velocity;
        } else {                                        // bird eğer istediğimiz sınırın altına inerse 2 ye yani oyun bitimine yönlendiriyor.


            // To play hitting voice
            if(gameState == 1) {
                engelecarptıvoice.play();
                engelecarptıvoice.setVolume(0.7f);
            }
            // -------

            gameState = 2;

            // engele carptıktan 1 saniye sonra gameover music calacak
            Timer.schedule(new Timer.Task() {
                @Override
                public void run() {
                    gameovervoice.play();
                    gameovervoice.setVolume(0.6f);
                }
            }, delay1);

            // ------------------
        }
    }
    else if (gameState == 2) {    // Oyun bittiyse sonra tekrar dokunulursa oyunu tekrar başlatabiliriz


        // Drawing stage 2, means game over screen

        Gdx.input.setInputProcessor(stage);


        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());

        stage.getBatch().begin();                                                                      // stage nin background u ayarlama
        stage.getBatch().draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());  // stage nin background u ayarlama
        fonth.draw(stage.getBatch(),"Highscore: "+String.valueOf(highscore),2*Gdx.graphics.getWidth()/7,2*Gdx.graphics.getHeight()/3);  // stage üzerine bitmapfont yazdırma
        font.draw(stage.getBatch(),"Current score: "+String.valueOf(score),2*Gdx.graphics.getWidth()/7,4*Gdx.graphics.getHeight()/8);
        font.setColor(Color.RED);
        stage.getBatch().end();                                                                        // stage nin background u ayarlama

        stage.draw();



        if(score > highscore){      // highscore güncellemesi
            highscore = score;

            preferences.putInteger("savedhighscore",highscore);
            preferences.flush();

        }

        // -------------------
        //    +
        //   ++
        //  ++++
    }

    batch.draw(bird, birdX, birdY, Gdx.graphics.getWidth() / 15, Gdx.graphics.getHeight() / 10);
    font.draw(batch, String.valueOf(score), 100, 200);  // skoru görmek için yazdırmamız lazımdı. konumu x 100, y 200 belirttik

    batch.end();

    birdCircle.set(birdX + Gdx.graphics.getWidth()/25, birdY + Gdx.graphics.getHeight()/20, Gdx.graphics.getWidth()/48); // kuşun circle tanımı


    for (int i = 0; i < 4; i++) {


        if(Intersector.overlaps(birdCircle,engelBoyu1Rectangle[i]) || Intersector.overlaps(birdCircle,engelBoyu1Rectangle2[i]) || Intersector.overlaps(birdCircle,engelBoyu1Rectangle3[i]) || Intersector.overlaps(birdCircle,engelBoyu1Rectangle4[i]) || Intersector.overlaps(birdCircle,engelBoyu1Rectangle5[i]) || Intersector.overlaps(birdCircle,engelBoyu2Rectangle[i]) || Intersector.overlaps(birdCircle,engelBoyu2Rectangle2[i]) || Intersector.overlaps(birdCircle,engelBoyu2Rectangle3[i]) || Intersector.overlaps(birdCircle,engelBoyu2Rectangle4[i]) || Intersector.overlaps(birdCircle,engelBoyu2Rectangle5[i]) ){

            // çarpıtıgı anda ses calacak sonra gamestate 2 olacak ve sesi kesecek

            if(gameState == 1) {
                engelecarptıvoice.play();
                engelecarptıvoice.setVolume(0.7f);
            }

            // -------

            gameState = 2;

            // engele carptıktan 1 saniye sonra gameover music calacak

            Timer.schedule(new Timer.Task() {
                @Override
                public void run() {
                    gameovervoice.play();
                    gameovervoice.setVolume(0.6f);
                }
            }, delay1);

            // ------------------
        }
    }
}

我上传了我的渲染方法

0 个答案:

没有答案