Using the XNA API
The following steps are a guide to using the Microsoft Advertising SDK for Windows Phone to integrate advertising into Windows Phone XNA games.
Important |
|---|
| The purpose of the sample code in this topic is to illustrate a concept and related syntax. The sample does not include all the code that is found in a full production system. For example, data validation and error handling code is not included. Technical support is not available for this sample or for any included source code. |
A complete working game, with code for using geo location ad targeting, ad borders, and the events AdRefreshed and ErrorOccurred is available for download at code.microsoft.com.
Prerequisites
-
Microsoft Visual Studio 2010 (or Microsoft Visual Studio 2010 Express)
-
See Creating a Windows Phone Game or Library Project for the steps that must be completed to create a Windows Phone Game (4.0).
Integrate an Ad into a Windows Phone XNA Game
-
In Visual Studio Solution Explorer, under your project, right-click References, and then click Add Reference….
-
In the Add Reference dialog box, click the .NET tab. Select the Microsoft.Advertising.Mobile.Xna.dll file, and click OK.
-
In the Solution Explorer panel, you will see Microsoft.Advertising.Mobile.Xna.dll listed in the References folder of your project.
-
Open the Game1.cs source code file, and add the following code in the
usingsection of Game1 class code.using Microsoft.Advertising.Mobile.Xna; -
Next you will add the variables that will store the game’s DrawableAd object. To begin, find the following variable definitions of the Game1 class. These are automatically generated as part of the
Game1class.public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch;After the line of code
SpriteBatch spriteBatch;, add the following line of code to declare thebannerAdvariable that will hold theDrawableAdobject.DrawableAd bannerAd;The global class variable declarations of the
Game1()class will read as follows:public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; DrawableAd bannerAd; -
Next, you will initialize an
AdGameComponentobject, and then you will add it to the game’sComponentobject.Find the Initialize method that was automatically generated. It has the following code:
protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); }After the // TODO add the following code to initialize the
AdGameComponentobject. For documentation about the overloadedInitializemethod, see the topic AdGameComponent.Initialize.// Initialize the AdGameComponent and add it to the game’s Component object AdGameComponent.Initialize(this, "test_client"); Components.Add(AdGameComponent.Current);At this point, your Initialize method will look like this:
protected override void Initialize() { // TODO: Add your initialization logic here // Initialize the AdGameComponent and add it to the game’s Component object AdGameComponent.Initialize(this, "test_client"); Components.Add(AdGameComponent.Current); base.Initialize(); } -
Next you will add your own
CreateAdmethod to theGame1class. In thisGame1.CreateAd()method you will put all the code necessary to define the banner ad for your game. Later, you will write the code necessary to make a call to this method from theGame1.Initialize()method.Scroll down to the end of the
Game1class, which is after theDrawmethod. That code looks like this:/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here base.Draw(gameTime); } }After the
Drawmethod close bracket, and before theGame1class close bracket, add the following code for the body of yourCreateAdmethod./// <summary> /// Create a DrawableAd with desired properties. /// </summary> private void CreateAd() { // Create a banner ad for the game. } -
Next, after the line of code,
// Create a banner ad for the game,add the code that will define the dimensions of your banner ad.int width = 480; int height = 80; int x = (GraphicsDevice.Viewport.Bounds.Width - width) / 2; // centered on the display int y = 5;At this point, your CreateAd method looks like this.
private void CreateAd() { // Create a banner ad for the game. int width = 480; int height = 80; int x = (GraphicsDevice.Viewport.Bounds.Width - width) / 2; // centered on the display int y = 5; } -
To complete your
CreateAdmethod, you will create theDrawableAdobject. Add the following code to theCreateAdmethod.bannerAd = AdGameComponent.Current.CreateAd(“Image480_80”, new Rectangle(x, y, width, height), true);
So your complete CreateAd method will look like this.
private void CreateAd() { // Create a banner ad for the game. int width = 480; int height = 80; int x = (GraphicsDevice.Viewport.Bounds.Width - width) / 2; // centered on the display int y = 5; bannerAd = AdGameComponent.Current.CreateAd(“Image480_80”, new Rectangle(x, y, width, height), true); } -
Now you will add a call to the
CreateAdmethod in theInitializemethod forGame1.Add the code
CreateAd()to the Initialize method so the complete Initialize method looks like this.protected override void Initialize() { // Initialize the AdGameComponent with your ApplicationId and add it to the game. AdGameComponent.Initialize(this, “test_client”); Components.Add(AdGameComponent.Current); // Create an actual ad for display. CreateAd(); base.Initialize(); } -
Build and run your game and you will see a sample test ad.
See Also
Send feedback about this documentation to adsfdbk@microsoft.com.
Publication date: 2012-03-27
Important