1

我正在制作特定于 android 的游戏,并且想使用 android API。但我想知道,是否有可能有一个类似 android 的菜单,然后从那里调用 libGdx?我已经尝试在 android 部分调整清单,所以 android 活动(菜单)是 LAUNCHER 活动并首先被调用。然后我想我可以有一个意图启动 ApplicationListener,但它不起作用。我究竟做错了什么?甚至可能吗?

4

2 回答 2

1

这很简单:

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

LoadingScreenActivity 是,你猜怎么着,一个活动。

// 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 的完整加载屏幕活动。它应该可以工作(只需删除您没有的 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();
        }
    }
}
于 2013-09-12T13:51:01.673 回答
1

如果有帮助,您可以使用 Libgdx 中的 Android UI 元素。这样,您可以获得您想要的行为,而不是您想要的方法。

http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements

http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup

于 2013-09-12T13:17:26.367 回答