简体   繁体   中英

Admob integration failing thread to work properly on AndEngine based app

I'm building an android game powered by AndEngine game framework.
I'm using the following code to inegrate with Admob:

@Override
protected void onSetContentView() {
    mRenderSurfaceView = new RenderSurfaceView(this, mEngine);
    mRenderSurfaceView.applyRenderer();
    setContentView(R.layout.main);

    final FrameLayout frameLayout = new FrameLayout(this);
    final FrameLayout.LayoutParams frameLayoutLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                                         FrameLayout.LayoutParams.FILL_PARENT);


    AdView adView = new AdView(this, AdSize.BANNER, "XXXXXXX");
    adView.refreshDrawableState();
    adView.setVisibility(AdView.VISIBLE);
    final FrameLayout.LayoutParams adViewLayoutParams =
        new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                     FrameLayout.LayoutParams.WRAP_CONTENT,
                                     Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);

    AdRequest adRequest = new AdRequest();
    adRequest.addTestDevice(AdRequest.TEST_EMULATOR);       
    adRequest.addTestDevice(Secure.ANDROID_ID);
    adView.loadAd(adRequest);

    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
        new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
    frameLayout.addView(adView, adViewLayoutParams);

    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

in the game, when a ball is created it makes a fading-in animation
I made with a thread:

    new Thread(new Runnable() {

        public void run() {
            mBody.setType(BodyType.StaticBody);
            mSprite.setAlpha(0.0f);
            try {
                while(mSprite.getAlpha() < 1.0f) {
                    mSprite.setAlpha(mSprite.getAlpha() + 0.01f);
                    Thread.sleep(3);
                }

                mBody.setType(BodyType.DynamicBody);
                mBody.setLinearVelocity(new Vector2(0, 10));

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }).start();

The problem is:
the animation works perfectly, BUT
when I'm adding the Admob code the Sprite appears for a second
and then, just disappearing.

it seems to me there is a problem between this two "chunks" of code.
but I can't put my finger on the solution or even what causing this problem to occur.

I only know that the animation is not working when the Admob code is combined in my app.

I'd like to know why and how to solve it. thank you guys

I recommend you to use an XML file to put your admin. Its cleaner, easy to use and according to my own tests it is faster than overriding onsetcontentview. In order to achieve that you need to extend LayoutGameActivity (there is also a simple version of that class. SimpleLayoutActivity if I'm not wrong)

I'll improve the answer when I get a computer.

This could be completely unrelated, but I had a problem with randomly disappearing Sprites recently, though not connected with Admob. The problem went away when I added the following line to the place where I set the Engine up:

engineOptions.getRenderOptions().disableExtensionVertexBufferObjects(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM