Flash Ad Manager enables Flash Platform developers to manage ads in their projects remotely via XML.
This project lets developers remotely switch between several embedded ads or alternatively serve custom ads from their own private server. Ads appear during the 'preloading' stage of an application. Flash Ad Manager consists of two core parts, the AdManager class and an XML configuration file.
Flash Ad Manager works by 'calling home' to retrieve an XML file which is parsed to determine which ad type should be invoked. Ad types are defined simply as being custom or embedded. A custom ad is either a user created SWF or image which gets downloaded and displayed by the AdManager. An embedded ad can be anything embedded at compile time such as a custom graphic or animation to an alternate ad API such as MochiAds or GameJacket.
Flash Ad Manager may be best thought of as a system for 'self serving' ads without relying on a third party API like MochiAds. The downloading and display of a custom graphic makes up the bulk of the code behind AdManager.
A primary design goal has been to encapsulate as much of the inner working behind a simple to use API.
XML
This example shows the structure of the configuration XML which AdManager attempts to download from a server of your choosing. By editing this file a user may remotely change which ad type should be instantiated by changing use_ad_type to either CUSTOM or EMBEDDED.
<?xml version='1.0' encoding='utf-8'?>
<root>
<application>
<ad_disabled_domains>
<domain>ad-disabled-domain1.com</domain>
<domain>ad-disabled-domain2.com</domain>
</ad_disabled_domains>
<use_ad_type>CUSTOM</use_ad_type>
<ad type="CUSTOM">
<ad_path>http://www.mysite.com/MyCustomAd.swf</ad_path>
<ad_link>http://www.mysite.com/</ad_link>
<min_ad_seconds>5</min_ad_seconds>
</ad>
<ad type="EMBEDDED">
<ad_name>MochiAds</ad_name>
</ad>
</application>
</root>
AdManager
The following is an example of a document class which instantiates the AdManager class, passing the URL of the configuration XML.
package {
import flash.display.MovieClip;
import com.ahrooga.flash_ad_manager.AdManager;
/**
* This is a sample document class which attempts to explain the various methods of the AdManager class.
*/
public class Main extends MovieClip {
private var adManager:AdManager;
public function Main() {
/* placeHolder is simply a DisplayObject which will be displayed until an ad type is resolved OR for the duration of the application's
* preloading if the configuration XML is inaccessible. It can be any type of DisplayObject exported in Frame 1. In this example the class,
* PlaceHolder, is a library asset in Example.fla. Placeholders can be positioned with their x & y properties relative to the stage.
* In this example our placeholder is centered relative to the stage.*/
var placeHolder:MovieClip = new PlaceHolder();
placeHolder.x = (stage.stageWidth - placeHolder.width) * 0.5;
placeHolder.y = (stage.stageHeight - placeHolder.height) * 0.5;
/* Here we instantiate the AdManager class passing 3 parameters. A reference to a function, startApp, which will initialize the rest
* of the application after the application has finished downloading. A place holder display object (defined above) and a URL pointing
* to our XML file.*/
adManager = new AdManager(startApp, placeHolder, "http://www.mysite.com/AdManagerConfig.xml");
/* Optionally we can hard-code an ad disabled domain using addDisabledDomain. If AdManager detects the domain which the application
* is currently loaded from matches a domain added using this method ads will not be loaded. Instead the place holder display object will
* remain visible for the duration of the preloader. Domain should include sub-domains including www.*/
adManager.addDisabledDomain("www.kongregate.com");
/* We can also customize the color of the preloader used by AdManager. The preloader always appears at the bottom of the application. */
adManager.preloaderBarColor = 0xFF2F00;
adManager.preloaderBaseColor = 0x000000;
/* Use registerEmbeddedAd to match an ad name to an ad init method. The ad name string must match the ad_name element
* defined in the loaded XML. AdManager will call the passed function if the XML calls for an embedded ad with a matching name. */
adManager.registerEmbeddedAd("MochiAds", initMochiAds);
adManager.registerEmbeddedAd("GameJacket", initGameJacket);
/* Finally, the AdManger instance MUST be added to the stage. AdManager will not initialize otherwise. All opitions must be set BEFORE
* adManager is added to the stage.*/
addChild(adManager);
}
private function initMochiAds():void {
trace("init MochiAds!");
}
private function initGameJacket():void {
trace("init GameJacket!");
}
private function startApp():void {
removeChild(adManager);
adManager = null;
trace("startApp");
/* Game/Application initialize code goes here. All ad API's should be configured to fire this method when finished. *
* This is the application's main startup method which should initialize the rest of the program. */
}
}
}