A Phaser plugin for providing nice ads integration in your phaser.io game
npm install @azerion/phaser-ads
`
Next up you'd want to add it to your list of js sources you load into your game:
`html
`
You could also opt for using the (free) jsdelivr cdn:
`html
`
After adding the script to the page you can activate it by enabling the plugin:
`javascript
game.add.plugin(PhaserAds.AdManager);
`
Usage
-----
First thing you need to do after loading the plugin is attaching a provider to the adManager. PhaserAds comes pre-compiled with 4 providers for you to choose from:
- Gamedistribution.com
- IMA SDK
- Cocoon.io
- Cordova HeyZap (wrapping your game with Cordova? Want HeyZap ads? Then this is your provider)
$3
If you already have an account on Gamedistribution.com you can skip this introduction if not, head on over to gamedistribution.com and sign up for a free account.
Once you're signed up you can check out this guide for settings up a game. This is important because this will supply you with a gameId, which you need to supply to the plugin.
So when you have your gameId you can start by registering the provider to the plugin:
`javascript
// Let's create a new provider, first argument should be the game, second should be the ad tag URL
var provider = new PhaserAds.AdProvider.GameDistributionAds(
game, // Your Phaser game instance
'2d77cfd4b1e5487d998465c29de195b3' // Your gameId
);
game.ads.setAdProvider(provider);
`
After this it's as easy as calling:
`javascript
game.ads.showAd();
`
$3
A provider can use any number of arguments configured in order to make it work, it all depends on the implementation that was made by the developer. For our IMA Provider you can create one like this:
`javascript
// Let's create a new provider, first argument should be the game, second should be the ad tag URL
var provider = new PhaserAds.AdProvider.Ima3(
game,
'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&correlator'
);
game.ads.setAdProvider(provider);
`
Now all you need to do is request an ad, and add an event listener that is called when the ad is completed/skipped/finished/done playing.
`javascript
game.ads.onContentResumed.addOnce(function() {
// This gets called when the ad is complete
game.state.start('NextState');
});
// Here we request the ad
game.ads.showAd();
``