Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Game development, HTML5, Javascript, Monetize and Phaser.

When you create an HTML5 game, sooner or later you will want to monetize it. If you plan to add ads, it can be quite a pain since there isn’t that much information around the web about HTML5 content and ads. DoubleClick network seems to be a good choice and there are good news because Orange Games developed a plugin for providing nice ads integration in your Phaser games, have a look at this example:
You may have to reload the iframe to see it back in action. You can find all the required docs in the official GitHub page, anyway here is the core of the integration:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no" />

    <title>Phaser Advertisement example</title>

    <script type="text/javascript" src="//imasdk.googleapis.com/js/sdkloader/ima3.js"></script>

    <!-- Include Phaser Crisp Text JS -->
    <script type="text/javascript" src="../build/phaser.min.js"></script>
    <script type="text/javascript" src="../build/phaser-ads.js"></script>

    <style type="text/css">
        body {
            margin: 0 auto;
            padding: 0;
        }

        #game-container, #ad-container {
            position: absolute;
            top: 0px;
            left: 0px;
            width: 800px;
            height: 600px;
        }
    </style>

    <!-- Game we want to track -->
    <script type="text/javascript">
        var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game-container', { create: create, preload: preload, resize: resize});
        Phaser.Device.whenReady(function () {
            game.plugins.add(Fabrique.Plugins.AdManager);
        });

        function preload() {

        }

        function create(){
            //Set the ad provider, we use google AdSense (ima 3 sdk)
            game.ads.setAdProvider(new Fabrique.AdProvider.AdSense(
                    game,
                    'game-container',
                    'ad-container',
                    '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&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator='
            ));

            //Content paused event is fired when the content (game) should be paused, and the ad will be played
            game.ads.onContentPaused.add(function () {
                console.log('Started playing add');
            });

            //This is fired when the ad is finished playing and the content (game) should be resumed
            game.ads.onContentResumed.add(function () {
                console.log('Finished playing add');
            });

            //This is how we request an add
            game.ads.requestAd();

            //the actual game content, behind the ad
            var text = game.add.text(game.width / 2, game.height / 2, 'If you see this, there is no Ad beeing displayed', {
                font: '30px Arial',
                fill: '#ffffff'
            });
            text.anchor.set(0.5);
        }

        function resize() {
            console.log('resized');
        }
    </script>
</head>
<body>
<div id="content-wrapper">
    <div id="game-container"></div>
    <div id="ad-container"></div>
</div>
</body>
</html>
Let me explain highlighted lines: Line 45: the game itself. Line 46: the id of game container. Line 47: the id of ad container. Line 48: the tag of your video ad. It’s not that easy to generate a working tag for video ads, so let me know if you need a tutorial about it. I think I’ll try this service in a game I am about to publish.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.