3

我是 Android 开发的菜鸟,我想使用 AndEngine 开发一个游戏并使用 SherlockActionBar 进行导航。问题是 AndEngine 需要我扩展 SimpleBaseGameActivity 而 SherlockActionBar 需要扩展 SherlockActivity。我正在尝试使用嵌套类方法,但我不知道如何在 OnCreate 期间使 AndEngine 初始化。任何帮助是极大的赞赏。

--EDIT-- 我已经修改了 ActionBarSherlock 示例中的静态附件演示,但仍然无法弄清楚如何使其工作,因为现有的 AndEngine 演示代码中没有 OnCreate 方法。

public class WABActivity extends SimpleBaseGameActivity implements IOnMenuItemClickListener, OnCreateOptionsMenuListener {
ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);

private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;

protected static final int MENU_RESET = 0;
protected static final int MENU_QUIT = MENU_RESET + 1;

private Camera mCamera;

protected Scene mMainScene;

private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mFaceTextureRegion;

private Font mFont;

protected MenuScene mMenuScene;

@Override
public EngineOptions onCreateEngineOptions() {
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}

@Override
public void onCreateResources() {
    FontFactory.setAssetBasePath("font/");

    final ITexture fontTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
    this.mFont = FontFactory.createFromAsset(this.getFontManager(), fontTexture, this.getAssets(), "Plok.ttf", 48, true, android.graphics.Color.WHITE);
    this.mFont.load();

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box_menu.png", 0, 0);
    this.mBitmapTextureAtlas.load();

}

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mMenuScene = this.createMenuScene();

    /* Just a simple scene with an animated face flying around. */
    this.mMainScene = new Scene();
    this.mMainScene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

    final Sprite face = new Sprite(0, 0, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
    face.registerEntityModifier(new MoveModifier(30, 0, CAMERA_WIDTH - face.getWidth(), 0, CAMERA_HEIGHT - face.getHeight()));
    this.mMainScene.attachChild(face);

    setTheme(R.style.Theme_Sherlock); //<--Not sure if this goes here
    mSherlock.setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW); //<--Not sure if this goes here
    mSherlock.setContentView(???); //<--How do I set ContentView? Do I need to set contentview?

    return this.mMainScene;
}

@Override
public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
    if(pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if(this.mMainScene.hasChildScene()) {
            /* Remove the menu and reset it. */
            this.mMenuScene.back();
        } else {
            /* Attach the menu. */
            this.mMainScene.setChildScene(this.mMenuScene, false, true, true);
        }
        return true;
    } else {
        return super.onKeyDown(pKeyCode, pEvent);
    }
}

@Override
public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
    switch(pMenuItem.getID()) {
        case MENU_RESET:
            /* Restart the animation. */
            this.mMainScene.reset();

            /* Remove the menu and reset it. */
            this.mMainScene.clearChildScene();
            this.mMenuScene.reset();
            return true;
        case MENU_QUIT:
            /* End Activity. */
            this.finish();
            return true;
        default:
            return false;
    }
}

protected MenuScene createMenuScene() {
    final MenuScene menuScene = new MenuScene(this.mCamera);

    final IMenuItem resetMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_RESET, this.mFont, "RESET", this.getVertexBufferObjectManager()), new Color(1,0,0), new Color(0,0,0));
    resetMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    menuScene.addMenuItem(resetMenuItem);

    final IMenuItem quitMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_QUIT, this.mFont, "QUIT", this.getVertexBufferObjectManager()), new Color(1,0,0), new Color(0,0,0));
    quitMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    menuScene.addMenuItem(quitMenuItem);

    menuScene.buildAnimations();

    menuScene.setBackgroundEnabled(false);

    menuScene.setOnMenuItemClickListener(this);
    return menuScene;
}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    return mSherlock.dispatchCreateOptionsMenu(menu);
}

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        sub = menu.addSubMenu("Options");
        sub.add(0, 1, 0, "Settings");
        sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);      
        return true;
    }

    @Override //<-- Giving Error: must override or implement a supertype method
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home || item.getItemId() == 0) {
            return false;
        }
        if(item.getItemId()==1){
            //SETTINGS SCREEN//
            sub.removeItem(1);
            sub.removeItem(2);
            sub.removeItem(3);
            sub.removeItem(4);
            sub.removeItem(5);
            sub.add(0, 5, 0, "Home");
            sub.add(0, 2, 0, "High Scores");
            sub.add(0, 3, 0, "Help");
            sub.add(0, 4, 0, "About");
            //mMode = startActionMode(new AnActionModeOfEpicProportions());
            }

        }


}
4

1 回答 1

1

查看 ActionBarSherlock 演示代码中包含的静态附件示例。静态附件正是为此目的而存在的。

于 2012-10-25T21:57:47.247 回答