Skip to content
Random game

Phaser games

Last updated 2026-11-19

This guide uses Phaser 3. The SDK calls go in your scenes: loading in the preload scene, gameplay in the game scene.

The script tag

Add the SDK before Phaser and your game in index.html:

<script src="https://games.foony.com/sdk/v1.js"></script>
<script src="phaser.min.js"></script>
<script src="game.js"></script>

Start the SDK, then the game

const sdk = window.FreeGamesSdk;

sdk.init().then((settings) => {
  sdk.loadingStart();
  const game = new Phaser.Game({
    type: Phaser.AUTO,
    parent: 'game',
    width: 1280,
    height: 720,
    scale: {mode: Phaser.Scale.FIT, autoCenter: Phaser.Scale.CENTER_BOTH},
    scene: [PreloadScene, MenuScene, PlayScene],
  });
  game.sound.mute = settings.isMuted;
  sdk.audio.onMuteChange((isMuted) => {
    game.sound.mute = isMuted;
  });
  sdk.pause.onPause(() => game.pause());
  sdk.pause.onResume(() => game.resume());
});

Phaser.Scale.FIT keeps your aspect ratio and fills as much of the frame as it can. For a game that can use any shape, Phaser.Scale.RESIZE fills it all.

Loader progress

class PreloadScene extends Phaser.Scene {
  constructor() {
    super('preload');
  }

  preload() {
    this.load.on('progress', (value) => sdk.loadingProgress(value));
    this.load.on('complete', () => sdk.loadingFinished());
    this.load.atlas('sprites', 'img/sprites.webp', 'img/sprites.json');
    this.load.audio('music', 'audio/music.ogg');
  }

  create() {
    this.scene.start('menu');
  }
}

Gameplay events

class PlayScene extends Phaser.Scene {
  constructor() {
    super('play');
  }

  create() {
    sdk.gameplayStart();
  }

  async onPlayerDied() {
    sdk.gameplayStop();
    sdk.gameEnd('loss');
    this.scene.pause();
    await sdk.commercialBreak();
    this.scene.restart();
  }

  onQuitPressed() {
    sdk.gameplayStop();
    this.scene.start('menu');
  }
}

scene.restart() runs create() again, which calls gameplayStart(). Every way out of the play scene calls gameplayStop() once.

Rewarded ads

async onWatchAdPressed() {
  const {rewarded} = await sdk.rewardedBreak();
  if (rewarded) {
    this.registry.inc('lives', 1);
  }
}

Before you upload

  • Build with your bundler, then ZIP the output folder’s contents (dist/ for Vite).
  • Set your bundler’s base path to relative (base: './' in Vite), so asset paths don’t start with /.
  • Load the build in the Inspector.