Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

AdRotator for Windows Phone XNA

0.00/5 (No votes)
24 Oct 2012 1  
AdRotator for Windows Phone XNA

Icon_1_purple image

AdRotator for Windows Phone XNA V0.3 Release

Hot on the heels of the Silverlight article for AdRotator is the documentation for the XNA version, to save repeating myself I’d suggest you read that first to get the general look and feel of AdRotator before continuing. I’m only going to go over the XNA implementation parts in this article.

In some ways, the XNA implementation is even easier than Silverlight because we don’t have to worry about all the GUI stuff, if you are doing a SilverXNA project (Silverlight / XNA integration) then either route will work fine (or both Open-mouthed smile).

Basic Nuts and Bolts

Just like with the Silverlight version, go and download all the binaries from the AdRotator codeplex site and copy them to your project, then add the references to the XNA libraries, you’ll notice there are far fewer of them because most don’t provide a native API so we have had to use the Web API for those.

Once they are in first, you have to initialize the control in the normal XNA “initialize” method:

protected override void Initialize()
{
	// TODO: Add your initialization logic here
	AdRotatorXNAComponent.Initialize(this);
	AdRotatorXNAComponent.Current.AdPosition = new Vector2(0, 720);
	AdRotatorXNAComponent.Current.SlidingAdDirection = SlideDirection.Bottom;
	AdRotatorXNAComponent.Current.DefaultSettingsFileUri = "defaultAdSettings.xml";
	AdRotatorXNAComponent.Current.SettingsUrl = 
                "http://adrotator.apphb.com/defaultAdSettingsXNALoc.xml";
	Components.Add(AdRotatorXNAComponent.Current);
	base.Initialize();
}

To ensure you also display the Ad in the correct orientation, we also recommend hooking up to the “OrientationChanged” event and ensuring that AdRotator is notified, so add the following to the Game Constructor:

Window.OrientationChanged += new EventHandler<EventArgs>(Window_OrientationChanged);

And then adding the corresponding method with the call to AdRotator:

void Window_OrientationChanged(object sender, EventArgs e)
{
	AdRotatorXNAComponent.Current.UpdateAdPostition();
}

And that’s pretty much it. Just be sure to add the “DefaultAdSettings.xml” config file (or whatever you call it) to the main project NOT the content project updating the above initialize logic accordingly, you can even configure the remote URL for the configuration if you so wish (optional).

Easy as Pie!

Local House AD

Since XNA is all about textures, your local house ad is just a texture image. Just drop it into your content project and then add the following lines to the above initialization logic:

AdRotatorXNAComponent.Current.DefaultHouseAdImage =
	Content.Load<Texture2D>(@"AdRotator");
AdRotatorXNAComponent.Current.DefaultHouseAdClick +=
	new AdRotatorXNAComponent.DefaultHouseAdClickEventHandler(Current_DefaultHouseAdClick);

Just make sure you also add the corresponding “Click” event handler to do what you want when the user clicks on the link, our samples simply do the following:

void Current_DefaultHouseAdClick()
{
	try
	{
		MessageBox.Show("You clicked on my Ad, thanks very much");
	}
	catch { }
}

But you could just as easily start up a web browser task or a marketplace detail task to show more details about your other games

Remote House Ad

The remote house Ad is just as simple, it’ll still use the same “Clicked” event as above (couldn’t remote that I’m afraid at the moment), but all you do is host an image on your website and update the XML configuration to point to it.

AdRotator will check the remote image, download it if it’s changed and cache it locally and then display it when asked.

In either case, the same default rules apply for house Ads, either configure them with a probability or don’t and you can use either a local or remote ad or BOTH.

XNA Couldn’t Be Simpler

As mentioned in the last post, we're still going to deliver Desktop and XBOX versions of the XNA control (XBOX without web of course just a local Ad, multiple in V2).

No XNA in Win 8 yet, so that will be XAML or monogame!

Again, we recommend at the very least you put AdDuplex in your XML configuration, it’s FREE and you get exposure everywhere and is accepted at all major and minor outlets just about everywhere, it’s great as a primary or just as the fall back position.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here