2012-10-19 42 views
2

我曾经为我的应用使用SimpleBaseGameActivity,一切正常,但现在我想在应用中添加一些广告,所以我尝试使用LayoutGameActivity。但是我得到的渲染视图是完全黑色的,我不知道为什么。这是我的代码:AndEngine:使用LayoutGameActivity时的黑色屏幕

public class AcGame extends LayoutGameActivity { 
    [...] 
    @Override 
    protected int getLayoutID() { 
     return R.layout.ad; 
    } 

    @Override 
    protected int getRenderSurfaceViewID() { 
     return R.id.layout_render; 
    } 

    @Override 
    public EngineOptions onCreateEngineOptions() { 
     [...] 
     return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, resolution, camera); 
    } 

    @Override 
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception { 
     createResources(); 
     pOnCreateResourcesCallback.onCreateResourcesFinished(); 
    } 

    @Override 
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception { 
     pOnCreateSceneCallback.onCreateSceneFinished(createScene()); 
    } 

    @Override 
    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception { 
     pOnPopulateSceneCallback.onPopulateSceneFinished(); 
    } 

这就是我的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
    <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="top" 
      android:id="@+id/layout_ad" 
      > 
     <Button 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:text="Ad Sample"/> 
    </LinearLayout> 
    <org.andengine.opengl.view.RenderSurfaceView 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:id="@+id/layout_render" 
      /> 

</LinearLayout> 

当我尝试调试,我可以看到EngineOptions创建,但没有的LayoutGameActivity的方法之一被称为(onCreateResourcesonCreateSceneonPopulateScene)。有人能告诉我我错过了什么吗?

UPD:我发现,如果移动在XML向下按钮,场景显示:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
    <org.andengine.opengl.view.RenderSurfaceView 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:id="@+id/layout_render" 
      /> 
    <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="top" 
      android:id="@+id/layout_ad" 
      > 
     <Button 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:text="Ad Sample"/> 
    </LinearLayout> 

</LinearLayout> 

但现在我看不到我的按钮,所有好像我仍在使用SimpleBaseGameActivity。

回答

5

我找到了解决方案:如果要使用(Simple)LayoutGameActivity,则必须使用RelativeLayout。 此外,如果您使用LayoutGameActivity而不是SimpleLayoutGameActivity,请不要忘记使用回调!