我曾经在我的应用程序中使用 SimpleBaseGameActivity ,一切都很好,但现在我想在应用程序中添加一些广告,所以我尝试使用 LayoutGameActivity 。但我得到的渲染 View 完全是黑色的,我不知道为什么。这是我的代码:
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 的任何方法(onCreateResources
、onCreateScene
或 onPopulateScene
)。有人可以告诉我我错过了什么吗?
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。
最佳答案
我找到了解决方案:如果您想使用(Simple)LayoutGameActivity
,则必须使用RelativeLayout
。
另外,如果您使用 LayoutGameActivity
而不是 SimpleLayoutGameActivity
,请不要忘记使用回调!
关于android - AndEngine:使用LayoutGameActivity时黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12972803/