AdMob的间质的Cocos2D-X WP8 [英] AdMob Interstitial Cocos2d-x WP8

查看:129
本文介绍了AdMob的间质的Cocos2D-X WP8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以enyone告诉我如何在我的cocos2d-X的游戏叫AdMob的间质性场景之间?

Can enyone tell me how to call AdMob Interstitial between scenes in my cocos2d-x game?

我已经试过这的http:// robwirving .COM / 2014年/ 07/21 /调用-C-方法-C-的WinRT组件/ 的指导,但我不知道如何从科科斯类运行它。

I have tried this http://robwirving.com/2014/07/21/calling-c-methods-c-winrt-components/ guide, but i don't know how to run it from cocos classes.

有没有另一种方式,或者一些指导?

Is there any another ways, or some guides?

推荐答案

最近我做到了。你必须做一些事情。首先创建辅助类,这将有助于你调用本机的功能(我用这所有3个平台,但这里只是窗户电话):

I've recently made it. You have to do few things. First of all create helper class, which will help you calling native function (I use this for all 3 platforms, but here's just windows phone):

NativeHelper.h:

NativeHelper.h:

#ifndef  __NATIVE_HELPER_H_
#define  __NATIVE_HELPER_H_

#include <string>
#include <functional>
#include "cocos2d.h"

using namespace std;
USING_NS_CC;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
namespace cocos2d

{

    public delegate void CSharpShowInterstitialDelegate();

    public ref class WP8NativeEventHelper sealed

    {

    public:

        void WP8NativeEventHelper::SetCSharpShowInterstitialDelegate(CSharpShowInterstitialDelegate^ delegate){

            m_CSharpShowInterstitialDelegate = delegate;

        }

        void CallShowInterstitial();

    private:

        property static CSharpShowInterstitialDelegate^ m_CSharpShowInterstitialDelegate;

    };



}
#endif

class NativeHelper
{
public:
    static void showInterstitial(string adSdk);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    WP8NativeEventHelper^ wp8helper;
#endif

    //instance required only for setting callback
    static NativeHelper* getInstance();

    ~NativeHelper()
    {
        instanceFlag = false;
    }

private:

    static bool instanceFlag;
    static NativeHelper* instance;

    NativeHelper() {};

};

#endif // __NATIVE_HELPER_H_

所以。我们有专门的C ++ / CX类Wp8NativeEventHelper,它可以对话与C#。在这里,我们店的委托。

So. We have special C++/CX class Wp8NativeEventHelper, which can "talk" with C#. Here we store a delegate.

它是如何工作的:

  1. 在C#中呼吁SetCSharpShowInterstitialDelegate并传递一个委托给它,这将在静态属性被记住。
  2. 然后C ++ \ CX可以使用CallShowInterstitial调用它。

现在NativeHelperWP.cpp:

Now NativeHelperWP.cpp:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

#ifndef  __NATIVE_HELPER_WP_H_
#define  __NATIVE_HELPER_WP_H_

#include "NativeHelper.h"

void WP8NativeEventHelper::CallShowInterstitial(){

    if (m_CSharpShowInterstitialDelegate)

    {

        m_CSharpShowInterstitialDelegate->Invoke();

    }

}

bool NativeHelper::instanceFlag = false;
NativeHelper* NativeHelper::instance = NULL;

NativeHelper* NativeHelper::getInstance()
{
    if(!instanceFlag){
        instance = new NativeHelper();
        instanceFlag = true;
        instance->wp8helper = ref new WP8NativeEventHelper();
    }

    return instance;
}

void NativeHelper::showInterstitial(){
    NativeHelper::getInstance()->wp8helper->CallShowInterstitial();
}

#endif

#endif

下面是CallShowInterstitial的只是实现的。此外,在NativeHelper :: showInterstitial我们称之为C ++ / CX,后来调用C#。

Here is just an implementation of CallShowInterstitial. Also in NativeHelper::showInterstitial we're calling C++/CX, which later calls c#.

现在C#code(MainPage.xaml.cs中):

Now c# code (MainPage.xaml.cs):

之外的命名空间:

using GoogleAds;

里面的类:

  private InterstitialAd interstitialAd;

在构造函数中:

WP8NativeEventHelper helper = new WP8NativeEventHelper();
helper.SetCSharpShowInterstitialDelegate(showInterstitial);

,也可以创建showInterstitial功能:

and also create showInterstitial function:

public void showInterstitial() //we recreate interstitial each time, because otherwise it'll show only once, only new requests won't work
{
  interstitialAd = new InterstitialAd("MY_AD_UNIT_ID");
  AdRequest adRequest = new AdRequest();

  #if DEBUG
  // Enable test ads.
  adRequest.ForceTesting = true;
  #endif


  interstitialAd.ReceivedAd += OnAdReceived;
  interstitialAd.LoadAd(adRequest);
}

和最后OnAdReceived:

and finally OnAdReceived:

private void OnAdReceived(object sender, AdEventArgs e)
{
  System.Diagnostics.Debug.WriteLine("Ad received successfully");
  interstitialAd.ShowAd();
}

按照本指南设置AdMob的:的https:/ /developers.google.com/mobile-ads-sdk/docs/admob/wp/quick-start

现在让我们用这个。

在HelloWorldScene.h补充:

In HelloWorldScene.h add:

#include "NativeHelper.h"

在HelloWorldScene.cpp:

In HelloWorldScene.cpp:

NativeHelper::showInterstitial();

用同样的方法,可以显示AdMob的横幅例如/隐藏/改变位置(但它的马车,所以我使用的广告中介)。

The same way you can show/hide/change position of admob banner for example (however it's buggy so I'm using ad mediation).

这篇关于AdMob的间质的Cocos2D-X WP8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆