gpt4 book ai didi

android - 从 android 代码启动 libGdx

转载 作者:行者123 更新时间:2023-11-29 21:34:20 25 4
gpt4 key购买 nike

我正在制作特定于 android 的游戏,并且想使用 android API。但我想知道,是否有可能像 android 菜单一样,然后从那里调用 libGdx?我试过在 android 部分调整 list ,所以 android Activity (菜单)是 LAUNCHER Activity ,首先被调用。然后我想我可以用一个 Intent 启动 ApplicationListener,但它不起作用。我究竟做错了什么?有可能吗?

最佳答案

很简单:

// my call of the loading screen from inside a fragment
Intent intent = new Intent(getActivity(), LoadingScreenActivity.class);
startActivity(intent);

猜猜看,LoadingScreenActivity 是一个 Activity。

// the onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = false;
cfg.useCompass = false;

initialize(new LoadingScreen(), cfg);
}

而 LoadingScreen 就是您已经提到的 ApplicationListener

public class LoadingScreen implements ApplicationListener {
// create(), resize() and more
}

所以基本上你需要在 Activity 中嵌入 libgdx...

编辑:

这是我使用 libgdx 的完整加载屏幕 Activity 。它应该可以工作(只需删除您没有的 CustomApplication 内容)。

public class LoadingScreenActivity extends AndroidApplication {
private final static String LOG_TAG = LoadingScreenActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = false;
cfg.useCompass = false;

initialize(new LoadingScreen(), cfg);
}

private class LoadingScreen implements ApplicationListener {
private OrthographicCamera mCamera = new OrthographicCamera();
private SpriteBatch mBatch;
private Stage mStage;
private ShapeRenderer mProgressLine;

@Override
public void create() {
AssetManager manager = CustomApplication.getAssetManager();
manager.load("ic_launcher.png", Texture.class);

mStage = new Stage();
mProgressLine = new ShapeRenderer();

mBatch = new SpriteBatch();

mCamera.setToOrtho(false, DesignUtil.getScreenWidth(), DesignUtil.getScreenHeight());
mStage.setCamera(mCamera);
}

@Override
public void resize(int width, int height) {
mStage.setViewport(width, height, true);
}

@Override
public void render() {
if (CustomApplication.getAssetManager().update()) {
IntentUtil.startActivity(LoadingScreenActivity.this, GameActivity.class);
finish();
return;
}

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

// tell the camera to update its matrices.
mCamera.update();

// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
mBatch.setProjectionMatrix(mCamera.combined);

mBatch.begin();
mProgressLine.begin(ShapeRenderer.ShapeType.Rectangle);
mProgressLine.rect(0 + mStage.getWidth() / 5, mStage.getHeight() / 2, mStage.getWidth() - mStage.getWidth() / 2.5f, DesignUtil.percentToPixel(5, mStage.getHeight()));
mProgressLine.setColor(0, 1, 0, 1);
mProgressLine.end();
mProgressLine.begin(ShapeRenderer.ShapeType.FilledRectangle);
mProgressLine.filledRect(0 + mStage.getWidth() / 5, mStage.getHeight() / 2, (mStage.getWidth() - mStage.getWidth() / 2.5f) * CustomApplication.getAssetManager().getProgress(), DesignUtil.percentToPixel(5, mStage.getHeight()));
mProgressLine.setColor(0, 1, 0, 1);
mProgressLine.end();
mBatch.end();
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {
mBatch.dispose();
mStage.dispose();
mProgressLine.dispose();
}
}
}

关于android - 从 android 代码启动 libGdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18758236/

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