Cocos2d Android - 结合CCGLSurfaceView和其他视图(例如Admob或Imageview)

时间:2011-06-24 10:02:47

标签: android opengl-es admob cocos2d-android

我遇到了问题,要求合并CCGLSurfaceView与其他Android原生视图一起工作,例如ImageView。 我开始使用Cocos2D开发游戏,我打算稍后通过admob添加广告。因此,我想在顶部运行CCGLSurfaceView OpenGl 我的游戏视图),广告应显示在其下方。

我已经尝试了一些操作,比如手动(不是通过XML)将两个视图添加到相对布局,但不断获取null pointers并关闭我的应用程序。

我还尝试了this教程,解释了如何将admob集成到OpenGl SurfaceView中。但它仍然没有用,也许是因为我正在使用Cocos2d?

我不知道,有人可以帮我解决这个问题(也许是特定于cocos2d)吗?

1 个答案:

答案 0 :(得分:1)

我们需要你的代码(把你自己的基础链接放在整体上并没有多大帮助)

我的方法是在布局中定义所有这些

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:gravity="center"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout 
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            android:layout_alignParentTop="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        </LinearLayout>

        <android.pixelrain.opengl.GLSurfaceViewChipmunk android:id="@+id/composed"
            android:gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />

        <LinearLayout
            android:id="@+id/linearLayoutAd"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_alignParentBottom="true"
            android:orientation="vertical"
            android:gravity="center_horizontal"
        />

</RelativeLayout>

请注意,GLSurfaceViewChipmunk是您用cocos2d替换的内容。

然后在应用程序中

 private void InitAdView()
    {
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayoutAd);

        // Create the adView
        adView = new AdView(this, AdSize.BANNER, AppSettings.AdmobAppId);

        ...set your add listener

        // Add the adView to the layout
        layout.addView(adView);