2012-10-25 19 views
3

我是Android開發的小菜鳥,我想開發一款使用AndEngine並使用SherlockActionBar進行導航的遊戲。問題是AndEngine需要我擴展SimpleBaseGameActivity,而SherlockActionBar需要擴展SherlockActivity。我正嘗試使用嵌套類的方法,但我無法弄清楚如何在OnCreate中使AndEngine初始化。任何幫助是極大的讚賞。我已經使用ActionBarSherlock示例中的靜態附件演示進行了修飾,但仍然無法弄清楚如何使其工作,因爲現有的AndEngine演示代碼中沒有OnCreate方法。有沒有可能將AndEngine與SherlockActivity一起使用?

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

     } 


} 
+0

有興趣看看這是否可能。 –

回答

1

看看ActionBarSherlock演示代碼中包含的靜態附件示例。 靜態附件完全是爲此目的而存在的。

+0

你可以看看我的編輯並告訴我哪裏出錯了嗎? –

+0

您需要在您的活動中重寫OnCreate並在其中添加靜態附件代碼。 – jjhorgan

+0

它現在就像一種魅力。謝謝。 –

相關問題