libgdx在触摸时使用android特定代码

时间:2018-07-06 16:44:26

标签: java android android-studio libgdx cross-platform

我想在触摸屏幕时在android上显示admob广告,我去了一个教程(https://www.youtube.com/watch?v=cwAN4LMXo58)并尝试了一下,但没有成功,我正在尝试展示本地广告。 我尝试使用event只是为了让它在被调用时写给我,但不是运气。

Android启动器:

package com.mygdx.game;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.mygdx.game.MyGdxGame;

import java.util.Arrays;
import java.util.List;

public class AndroidLauncher extends AndroidApplication implements TempInterface{

    private InterstitialAd mInterstitialAd;
    private final int SHOW_ADS = 1;
    private final int HIDE_ADS = 0;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case SHOW_ADS:
                    System.out.println("show");
                    break;
                case HIDE_ADS:
                    System.out.println("hide");

                    break;
            }
        }
    };
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(), config);

        // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());

        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }


    @Override
    public void showAds(boolean show) {
        handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);

    }
}

core gameScreen(是第二个类,第一个是游戏,它会调用它)

public class GameScreen implements Screen {
 private final TempInterface tempInterface;
    boolean toggle;
 public GameScreen(final Game game, TempInterface tempInterface) {
        this.game = game;
        this.tempInterface = tempInterface;

    }

  Gdx.input.setInputProcessor(new InputAdapter() {
            @Override
            public boolean touchDown (int x, int y, int pointer, int button) {
                // your touch down code here

                tempInterface.showAds(toggle);
                toggle = !toggle;
};

和界面:

public interface TempInterface {
    public void showAds(boolean show);

}

0 个答案:

没有答案