3

I'm a noob to Android development and i want to develop a game you using AndEngine and use SherlockActionBar for navigation. The problem is that AndEngine requires me to extend SimpleBaseGameActivity and SherlockActionBar needs to extend SherlockActivity. I am attempting to use a nested class approach, but i can't figure out how to make AndEngine initialize during OnCreate. Any help is greatly appreciated.

--EDIT-- I've tinkered with the static attachment demo in the ActionBarSherlock examples, but still can't figure out how to make this work, since there is no OnCreate method in the existing AndEngine demo code.

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());
            }

        }


}
B. Money
  • 931
  • 2
  • 19
  • 56

1 Answers1

1

Look at the static attachment example included in the ActionBarSherlock demo code. Static attachment exists exactly for this purpose.

jjhorgan
  • 107
  • 1
  • 5