gpt4 book ai didi

java - Libgdx减少调用和优化游戏

转载 作者:行者123 更新时间:2023-11-30 01:04:50 26 4
gpt4 key购买 nike

我试图减少每次在我的游戏上调用屏幕时进行的调用次数,以使我的游戏更快,我注意到我在每个屏幕上都做了很多相同的计算。我该如何避免这种情况?

我几乎在每个屏幕上都这样做

public class ****Screen implements Screen {
@Override
public void show() {

float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
float gameWidth = 360;
float gameHeight = screenHeight / (screenWidth / gameWidth);
midPointY = (int) (gameHeight / 2);
cam = new OrthographicCamera();
cam.setToOrtho(true, gameWidth, gameHeight);
viewport = new FitViewport(gameWidth, gameHeight, cam);
viewport.apply();
stage = new Stage(viewport);
Gdx.input.setInputProcessor(stage);

但是我有一个 GameClass..我怎样才能在我的游戏类(下面)中实现上面的内容并且只需要调用它一次?...

public class Start extends Game {
@Override
public void create() {
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
float gameWidth = 360;
float gameHeight = screenHeight / (screenWidth / gameWidth);

assets = new AssetLoader();
assetManager = new AssetManager();
camera = new OrthographicCamera();
camera.setToOrtho(true, gameWidth, gameHeight);

//initialize screens here
mainMenu = new MenuScreen(this);
loadingScreen = new LoadingScreen(this);
gameScreen = new GameScreen(this);

.......

//call assets
AssetLoader.load();

//start mainmenu...
this.setScreen(mainMenu);
}

最佳答案

首先我必须告诉你实际上这不会加速你的游戏。如果您每 20 秒更换一次屏幕,那么这意味着游戏每 1200 帧计算 1 帧。但是我和你一样,在做游戏的时候真的在寻找最优化的方法。

我找到了这个案例的解决方案。您可以传递在所有屏幕中使用的对象,从游戏类到屏幕类。

屏幕类

public class MainMenuScreen implements Screen {

public MainMenuScreen(OrthographicCamera camera) {
this.camera=camera;
}

//...Rest of class omitted for succinctness.
}

游戏类

public class Starts extends Game {

OrthographicCamera camera;
public void create() {
camera=new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
this.setScreen(new MainMenuScreen(camera));// change screen and pass camera to new screen.
}

}

或者您甚至可以像这样通过整个游戏类(class)。

public class MainMenuScreen implements Screen {

final Starts game;
public MainMenuScreen(final Starts game) {
this.game = game;

}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

game.camera.update();
game.batch.setProjectionMatrix(game.camera.combined);

game.batch.begin();
game.font.draw(...);
game.font.draw(...);
game.batch.end();

if (Gdx.input.isTouched()) {
game.setScreen(new AnotherScreen(game));
dispose();
}
}

}

只需要在游戏类中这样调用即可。所以你可以在所有屏幕上使用游戏的阶段批处理字体等。

this.setScreen(new MainMenuScreen(this));

关于java - Libgdx减少调用和优化游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com