Click here to Skip to main content
15,881,709 members
Articles / Mobile Apps / Windows Phone 7

AdRotator for Windows Phone XNA

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Oct 2012CPOL3 min read 6.5K   2  
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:

C#
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:

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

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

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ZenithMoon Studios
United Kingdom United Kingdom
Long-time game developer / IT maniac.
By day I architect, design, build and deliver enriching Mixed Reality solutions to clients, bringing the work of AR/VR to light in new and interesting ways, by night I Masquerade as the Master Chief of ZenithMoon Studios, my own game development studio.

At heart, I am a community developer breaking down lots of fun and curious technologies and bringing them to the masses.

I'm also a contributor to several open-source projects, most notably, the Reality Toolkit and all the services provided by the Reality Collective, The Unity-UI-Extensions project, as well as in the past the AdRotator advertising rotator project for Windows and Windows Phone.

Currently, I spend my time fulfilling contracts in the Mixed Reality space (primarily for an XR experience firm called Ethar), writing books, technically reviewing tons of material and continuing my long tradition of contributing to open-source development, as well as delivering talks, but that goes without saying Big Grin | :-D

Mixed Reality MVP, Xbox Ambassador, MS GameDevelopment Ambassador & Best selling author:

[Accelerating Unity Through Automation](https://www.amazon.co.uk/Accelerating-Unity-Through-Automation-Offloading/dp/1484295072/ref=rvi_sccl_3/262-0817396-1418043)
[Mastering Unity 2D Game Development] (https://www.packtpub.com/game-development/mastering-unity-2d-game-development)
[Unity 3D UI Essentials] (https://www.packtpub.com/game-development/unity-3d-gui-essentials)

Comments and Discussions

 
-- There are no messages in this forum --