Skip to content

Phaser

AI Summary

A short gist of this page, generated for you.

Powered by AI. Summaries can miss nuance — when in doubt, read the page.

Phaser games use the standard script tag. The one thing to get right is the order: initialize the SDK first, then start Phaser.

#Phaser 3

Most common mistake

Calling new Phaser.Game({...}) at the bottom of index.html without waiting for FunOxSDK.init(). init() resolves right away, so waiting costs nothing and guarantees the SDK is ready before any scene uses it. Always start Phaser inside the .then() callback.

#index.html

html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>My Game</title>
  <style>
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body { background: #000; overflow: hidden; }
  </style>
</head>
<body>

  <!-- 1. Funox SDK first -->
  <script src="https://funox.com/sdk/v1/funox-sdk.js"></script>

  <!-- 2. Phaser -->
  <script src="phaser.min.js"></script>

  <!-- 3. Your scenes -->
  <script src="src/scenes/PreloadScene.js"></script>
  <script src="src/scenes/GameScene.js"></script>
  <script src="src/scenes/GameOverScene.js"></script>

  <!-- 4. Init the SDK, THEN start Phaser -->
  <script>
    FunOxSDK.init().then(startPhaser, startPhaser);

    function startPhaser() {
      new Phaser.Game({
        type: Phaser.AUTO,
        width: 480,
        height: 854,
        backgroundColor: '#000000',
        scene: [PreloadScene, GameScene, GameOverScene],
        scale: {
          mode: Phaser.Scale.FIT,
          autoCenter: Phaser.Scale.CENTER_BOTH
        }
      });
    }
  </script>

</body>
</html>

#GameScene.js

javascript
class GameScene extends Phaser.Scene {
  constructor() { super({ key: 'GameScene' }); }

  create() {
    var self = this;

    // Active play starts here — not in the preload scene
    FunOxSDK.gameplayStart();

    // Platform events
    FunOxSDK.on('pause', function () { self.scene.pause(); });
    FunOxSDK.on('resume', function () { self.scene.resume(); });
    FunOxSDK.on('mute', function () { self.sound.mute = true; });
    FunOxSDK.on('unmute', function () { self.sound.mute = false; });

    // Load the cloud save. Resolves with null for guests and new players.
    FunOxSDK.loadProgress().then(function (data) {
      if (data) self.applySave(data);
    });

    this.score = 0;
  }

  onLevelComplete() {
    var self = this;

    FunOxSDK.happyTime();
    FunOxSDK.saveProgress({ level: this.currentLevel, score: this.score })
      .catch(function () { /* guest player */ });

    // Ad break between levels — pause and mute first
    this.scene.pause();
    this.sound.mute = true;
    FunOxSDK.commercialBreak()
      .catch(function () {})
      .then(function () {
        self.sound.mute = false;
        self.scene.resume();
        self.loadNextLevel();
      });
  }

  onGameOver() {
    FunOxSDK.gameplayStop();

    // Remove listeners so they are not registered twice on restart
    FunOxSDK.offAll('pause');
    FunOxSDK.offAll('resume');
    FunOxSDK.offAll('mute');
    FunOxSDK.offAll('unmute');

    this.scene.start('GameOverScene', { score: this.score });
  }
}

#GameOverScene.js

javascript
class GameOverScene extends Phaser.Scene {
  constructor() { super({ key: 'GameOverScene' }); }

  init(data) {
    this.finalScore = data.score || 0;
  }

  create() {
    var self = this;

    this.add.text(240, 300, 'GAME OVER', { fontSize: '32px', fill: '#fff' }).setOrigin(0.5);
    this.add.text(240, 360, 'Score: ' + this.finalScore, { fontSize: '24px', fill: '#fff' }).setOrigin(0.5);

    // Rewarded ad — "Watch ad for an extra life"
    var rewardBtn = this.add.text(240, 440, 'Watch Ad for Extra Life', {
      fontSize: '18px', fill: '#ffdd00', backgroundColor: '#333', padding: { x: 10, y: 6 }
    }).setOrigin(0.5).setInteractive();

    rewardBtn.on('pointerdown', function () {
      self.sound.mute = true;
      FunOxSDK.rewardedBreak('extra_life')
        .then(function (result) {
          self.sound.mute = false;
          if (result.completed) {
            self.scene.start('GameScene'); // restart with the extra life
          }
        })
        .catch(function () {
          self.sound.mute = false;
        });
    });

    var playBtn = this.add.text(240, 520, 'Play Again', {
      fontSize: '20px', fill: '#fff'
    }).setOrigin(0.5).setInteractive();

    playBtn.on('pointerdown', function () {
      self.scene.start('GameScene');
    });
  }
}

#Checklist for Phaser 3

  • The SDK script tag comes before phaser.min.js and your game scripts
  • new Phaser.Game({...}) is called after FunOxSDK.init() settles, not at the top level
  • FunOxSDK.gameplayStart() is called in GameScene.create() — not in a boot or preload scene
  • FunOxSDK.gameplayStop() is called on game over or scene shutdown
  • FunOxSDK.commercialBreak() is called at a natural break, with the scene paused and sound muted around it
  • pause and resume are handled in the game scene
  • FunOxSDK.offAll() is called when leaving the game scene

#Phaser 2 / Community Edition

javascript
var BootState = {
  create: function () {
    var next = function () { game.state.start('Preload'); };
    FunOxSDK.init().then(next, next);
  }
};

var GameState = {
  create: function () {
    FunOxSDK.gameplayStart();
    FunOxSDK.on('pause', function () { game.paused = true; });
    FunOxSDK.on('resume', function () { game.paused = false; });
  },

  shutdown: function () {
    FunOxSDK.gameplayStop();
    FunOxSDK.offAll('pause');
    FunOxSDK.offAll('resume');
  }
};