从android代码启动libGdx

时间:2013-09-12 07:36:35

标签: android android-intent libgdx

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

2 个答案:

答案 0 :(得分:1)

如果有帮助,您可以使用Libgdx中的Android UI元素。这样,您可以获得所需的行为,但不能获得所需的方法。

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

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

答案 1 :(得分: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();
        }
    }
}