OpenX Loader ActionScript 3 Class for Adobe AIR

openx_air

Following my article on loading OpenX ads in Adobe AIR applications, I’ve written a very simple class to make it a little easier. This should be able to load in both image and HTML/text based banners with no problem.

This class will NOT run in the regular Flash Player. It requires Adobe AIR to function. Also, banner clicks from the Debug Launcher will NOT redirect properly (you’ll get a 404), but once the AIR app is compiled and installed links will work as they should and will be included in your OpenX stats.

Download OpenxLoader AS3 Class
Downloaded 274 times.

The Constructor Method

Extends the Sprite class.

OpenxLoader(w:Number, h:Number, url:String)

  • w: width of the HTMLLoader
  • h: height of the HTMLLoader
  • url: URL to load in the HTMLLoader (the code of this page should have your OpenX invocation code with all applicable zones)

Public Properties

  • html: Public reference to the HTMLLoader instance
  • height: Height of the HTMLLoader instance
  • width: Width of the HTMLLoader instance
  • url: URL to load in the HTMLLoader instance (automatically loads the new URL when changed)

Public Methods

  • refresh(): Reloads the content in the HTMLLoader instance

Basic Example

//Import the class
import us.studiochris.openx.OpenxLoader;

//Create a new instance of the class
var ads:OpenxLoader = new OpenxLoader(125, 125, "http://domain.com/open-x-ad-page.html");

//Add the OpenxLoader to the Stage
addChild(ads);

Auto-refreshing Example

//Import the class
import us.studiochris.openx.OpenxLoader;

//Create a new instance of the class
var ads:OpenxLoader = new OpenxLoader(125, 125, "http://domain.com/open-x-ad-page.html");

//Add the OpenxLoader to the Stage
addChild(ads);

//Create a new Timer to control the refresh rate
var t:Timer = new Timer(60000,1); // 1 minute refresh rate in milliseconds and we want to run it once

//Start the Timer
t.start();

//Add an Event Listener to the Timer and define the reloadAds function
t.addEventListener(TimerEvent.TIMER, reloadAds);

function reloadAds(e:TimerEvent):void
{
	//Call the refresh method of the OpenxLoader (has the effect of a rotating banner)
	ads.refresh();

	t.start(); //start the timer again so it'll call this function again in one minute
}

Flex 4 Framework Basic Example

The Syntax highlighter is wreaking havoc with the following code, but it should give you an idea on how to use the class. As an alternative, here’s the actual MXML file for the project.



		

				import us.studiochris.openx.OpenxLoader;

				private function init():void
				{
					var adloader:OpenxLoader = new OpenxLoader(600,600,"http://www.google.com");
					box.addChild(adloader);
				}

		
	
Add the studio|chris Feed to your RSS reader.

3 Responses to “OpenX Loader ActionScript 3 Class for Adobe AIR”

  1. Victor

    Hi,

    Is there any way to include a working example of this? I installed OpenX, created a campaign and got the banners to show via an html page, but I can get this to work in flex.

    I created a new flex project, put the class into the src folder and then copied and pasted the code examples you gave me, changing the URLS to correspond to my ads, but it still doesn’t work. Am I missing something that I need to do in Openx? I was getting errors at first, but then I got rid of them. The application would load, but not would come up. Even after packaging into an air app.

    Thanks for the code…now if I can only get it to work :/(

    • Hi Victor–

      Sent you an email a little while ago for a few more details. You shouldn’t need to do anything special at all. Basically, this just makes a mini web browser window without controls and loads in your ad page just as it would load in Firefox, Chrome, Safari, etc.

      Just remember this is set up for AIR, and not regular Flex apps. If you’re running a Flex app in the browser, and not an AIR app, you may be able to use a similar method, just not with AIR’s HTMLLoader class. This article may give you some clues. I’m not sure how the JavaScript delivery would work out though.

      ——

      For a rundown of how I’m successfully using OpenX using this AS3 class, I have my application set up as a separate Website (My App), and My App has zones set up to receive the ads.

      For the campaigns, each applicable campaign is linked to a specific zone belonging to My App using the specific campaign’s Linked Zones tab. Individual banners may need to have their Zone settings tweaked as well.

      For my ad page, I use the website invocation code (Inventory > Websites > My App > Invocation Code tab). From there, just use the tags OpenX provides to create your ad page.

      It should give you a single JavaScript to add to the head of your ad page:

      …and each zone for that website will have a line of JavaScript that tells OpenX which ad to load:

      The final results can be seen in my Brush Manager application.

  2. I’ve added an update with an example using the Flex open source framework. It compiles and runs perfectly with FlashDevelop using the new Flex 4.0 SDK.

Leave a Reply

Note: This post is over 7 months old. You may want to check later in this blog to see if there is new information relevant to your comment.