Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

D. Managing Banner Ads

Hadi Tavakoli edited this page Jun 14, 2018 · 4 revisions

To generate a new Banner Ad, you must have initialized the Admob ANE. A banner ad can be initialized and loaded over your Air app. As soon as you initialize the banner ad, the AdMobEvents.SIZE_MEASURED will be dispatched which will tell you the real size of the banner in pixels. But the ad won't be shown unless you load it first using the loadAd() method. When you load a banner, you must listen to the general event AdMobEvents.AD_LOADED to know when the ad is loaded. That's when the banner will be shown at position (0,0) of your Air stage. When the banner is shown, that's the time to move it to the position you like in your Air app.

Although you have control on the x,y position of the banner, you don't have direct control over the width, height. because the dimension of Banner ads are decided by Admob. So, you are limited to one of the following sizes:

  • BANNER
  • FULL_BANNER
  • LARGE_BANNER
  • LEADERBOARD
  • MEDIUM_RECTANGLE
  • SMART_BANNER_LANDSCAPE
  • SMART_BANNER_PORTRAIT
  • WIDE_SKYSCRAPER

These constants are found in ApiBannerAds

NOTE 1: You must be very careful on deciding which banner size you are picking. because if there's not enough space, the banner won't be shown at all. In practice, we have found BANNER, SMART_BANNER_LANDSCAPE and SMART_BANNER_PORTRAIT the best choices.

NOTE 2: Requesting an ad can be customized. To learn more about the possible properties, read here.

// don't forget to initialize the Admob first
// https://github.com/myflashlab/Admob-ANE/wiki/B.-Initialization-and-Listeners

AdMob.api.addEventListener(AdMobEvents.AD_LOADED, 				onAdLoaded);
AdMob.api.banner.addEventListener(AdMobEvents.SIZE_MEASURED, 	onBannerAdSizeReceived);

AdMob.api.banner.init("ca-app-pub-930840122057342/5256142323", ApiBannerAds.BANNER);

var adRequest:AdRequest = new AdRequest();
adRequest.testDevices = ["an array of device IDs so you can receive test Ads when developing!"];

/*
If you don't know how you can receive your device's ID, read here for Android:
https://firebase.google.com/docs/admob/android/targeting#test_ads
or here for iOS:
https://firebase.google.com/docs/admob/ios/targeting#test_ads
*/

AdMob.api.banner.loadAd(adRequest);

private function onAdLoaded(e:AdMobEvents):void
{
	if (e.adType == AdMob.AD_TYPE_BANNER)
	{
		// place the Ad at the center of your game, or anywhere else you wish!
		AdMob.api.banner.x = stage.stageWidth / 2 - AdMob.api.banner.width / 2;
		AdMob.api.banner.y = stage.stageHeight / 2 - AdMob.api.banner.height / 2;
	}
}