LibGDX如何使用视口支持不同的屏幕尺寸

时间:2017-03-20 23:26:24

标签: android libgdx viewport android-screen-support

我一直在寻找一种方法,可以让我在我的android libgdx应用程序中使用视口,但我找到的所有那些似乎都不起作用。我不使用舞台,所以我想知道这是不是我的问题。

例如,如果我将此代码添加到我的应用程序中:

private Viewport viewport;
private Camera camera;

public void create() {
    camera = new PerspectiveCamera();
    viewport = new StretchViewport(800, 480, camera);
}

@Override
public void resize(int width, int height) {
    viewport.update(width, height);
}

当我在手机上试用它时,它可以工作,但是当我在7英寸平板电脑上试用它时,我的应用程序中的所有对象都太大了,几乎就像我需要缩小一样。我尝试过很多视口,包括FitViewport和FillViewport,但它们似乎不起作用。我也尝试将StretchViewport中的800,480更改为800,600,因为这是我的背景图像大小,但它没有做任何事情。

这就是手机上的样子:

Phone Image

然后,在平板电脑上,它看起来像这样:

Tablet Image

如您所见,平板电脑放大了。

这是我的render()方法:

@Override
public void render () {

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

    if(Gdx.input.justTouched()){

        isGoingUp = !isGoingUp;

    }

    if(gameState == 1) {

        if(obstacleX[scoringObstacle] + obstacle.getWidth() < Gdx.graphics.getWidth() / 2 + balls[0].getWidth()){

            if(obstacleY[scoringObstacle] < Gdx.graphics.getHeight() && obstacleY[scoringObstacle] + obstacle.getHeight() > 0) {

                score++;

                if(score > highscore){

                    highscore++;

                    highscorePref.putInteger("highscore", highscore);

                    highscorePref.flush();

                }

                if(score >= 10 && score < 20){

                    obstacleVelocity = 11;//6

                }

                else if(score >= 20 && score < 30){

                    obstacleVelocity = 12;//7

                }

                else if(score >= 30 && score < 40){

                    obstacleVelocity = 13;//8

                }

            }

            if(scoringObstacle < numberOfObstacles - 1){

                scoringObstacle ++;

            }

            else{

                scoringObstacle = 0;

            }

        }

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

            if (obstacleX[i] < - obstacle.getWidth()){

                obstacleX[i] += numberOfObstacles * distanceBetweenObstacles;

                obstacleY[i] = (randomGenerator.nextFloat() - 0.5f) * (Gdx.graphics.getHeight() * 1.5f);

                if (obstacleY[i] > Gdx.graphics.getHeight() - 200 || obstacleY[i] + obstacle.getHeight() < 200){

                    obstacleY[i] = Gdx.graphics.getHeight() / 2 - obstacle.getHeight() / 2;

                }

            }

            else {

                obstacleX[i] = obstacleX[i] - obstacleVelocity;

            }

            batch.draw(obstacle, obstacleX[i], obstacleY[i]);

            if(score >= 20){

                if(obstacleX[3] < - obstacle.getWidth()){

                    bhGone = true;

                }

                if(bhGone) {

                    bhChance = randomGenerator.nextInt(2);

                    bhGone = false;

                }

                if(bhChance == 0){

                    batch.draw(blackHole, obstacleX[3] - distanceBetweenObstacles / 2 + blackHole.getWidth() / 2, Gdx.graphics.getHeight() / 2 - blackHole.getHeight() / 2);

                    blackHoleCircle.set(obstacleX[3] - distanceBetweenObstacles / 2 + blackHole.getWidth(), Gdx.graphics.getHeight() / 2, blackHole.getHeight() / 2);

                }

            }

            obstacleRectangles[i] = new Rectangle(obstacleX[i], obstacleY[i], obstacle.getWidth(), obstacle.getHeight());

            if (Intersector.overlaps(ballCircle, obstacleRectangles[i])) {

                diedByBh = false;

                gameState = 2;

            }

            else if(Intersector.overlaps(ballCircle, blackHoleCircle)){

                diedByBh = true;

                blackHoleCircle.set(0, 0, 2);

                gameState = 2;

            }

        }

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

            if (!isGoingUp) {

                if(ballY[i] >= 0) {

                    ballY[i] = ballY[i] - ballVelocity;

                }

            }

            else{

                if(ballY[i] <= Gdx.graphics.getHeight() - balls[0].getWidth()) {

                    ballY[i] = ballY[i] + ballVelocity;

                }

            }

        }

        if (ballState < 7) {

            ballState++;

        } else {

            ballState = 0;

        }

    }

    else if(gameState == 0){

        infoFont.draw(batch, "Tap to move up and down", Gdx.graphics.getWidth() / 4 - fontBack, Gdx.graphics.getHeight() / 2);

        if(Gdx.input.justTouched()){

            gameState = 1;

        }

    }

    else if(gameState == 2) {

        if(diedByBh){

            balls[ballState] = new Texture("nothing.png");

        }

        else {

            balls[ballState] = new Texture("splatteredball.png");

        }

        infoFont.draw(batch, "Tap to play again", Gdx.graphics.getWidth() / 4 - fontBack, Gdx.graphics.getHeight() / 2);

        highscoreFont.draw(batch, "High Score: " + String.valueOf(highscore), Gdx.graphics.getWidth() / 4 - fontBack, Gdx.graphics.getWidth() / 4 - 100);

        if(showAd) {

            showAd = false;

            adNumber = 1;

            adsController.showInterstitialAd();

        }

        else {

            if(!adNumDone) {

                if (adNumber < 15) {

                    adNumber++;

                } else {

                    showAd = true;

                }

                adNumDone = true;

            }

        }

        isGoingUp = true;

        if (Gdx.input.justTouched()) {

            if(Gdx.input.getY() < viewport.getWorldHeight() / 2){

                adsController.showRewardedVideoAd();

            }

            else {

                adNumDone = false;

                gameState = 1;
                startGame();
                score = 0;
                scoringObstacle = 0;

            }

        }

    }

    batch.draw(balls[ballState], Gdx.graphics.getWidth() / 2 + balls[0].getWidth(), ballY[ballState]);

    ballCircle.set(Gdx.graphics.getWidth() / 2 + balls[0].getWidth() + balls[ballState].getWidth() / 2, ballY[ballState] + balls[ballState].getHeight() / 2, balls[ballState].getHeight() / 2);

    scoreFont.draw(batch, String.valueOf(score), 50, Gdx.graphics.getHeight() - 50);

    batch.end();

}

任何想法都将不胜感激。提前谢谢!

0 个答案:

没有答案