Typescript window.onload not called after add requirejs for phaser game

I am trying to create a game with phaser and Typescript. I followed the instructions here and it worked at the beginning. Problems came when I tried to modulate my code with AMD and requirejs

index.html

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Resonate</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
    <script src="phaser.js"></script>
    <script src="http://requirejs.org/docs/release/2.1.20/minified/require.js" data-main="app"></script>
</head>
<body>
    <h1>RESONATE</h1>

    <div id="content"></div>
</body>
</html>

      

Player.ts

export class Player {

    color: string;

    constructor() {
        this.color = "#0000ff";
    }
}

      

app.ts

import player = require("Player");

class PhaserDemo {

    game: Phaser.Game;

    constructor() {
        this.game = new Phaser.Game(800, 600, Phaser.WEBGL, 'content', { preload: this.preload, create: this.create });
    }

    preload() {
        this.game.load.image('phaser_run', 'run.png');
    }

    create() {
        console.log("Before");
        var p = new player.Player();
        this.game.stage.backgroundColor = p.color;
    }
}

window.onload = () => {
    console.log("ONLOAD");
    var game = new PhaserDemo();
};

      

Now when I load it window.onload is never called, I tried following window.onload not calling function for Javascript solution, but I cannot get it to work in Typescript.

Also generated Javascript is created here in case you want to look at https://gist.github.com/koreus7/06bee4a30d22bdc76d62

+3


source to share


2 answers


for a Javascript solution, but I can't seem to get it to work in typescript.

Basically, if the window is already loaded, attaching to window.onload

will not call the attached function.



Check document.readyState === "complete"

. Alternatively use something like jquery ready: https://api.jquery.com/ready/

+2


source


I believe this is caused by the require.js loader, but I don't know that it looks a bit like jspm system.js, so we don't need to make sure the window loads because the loader has already provided this.



I just removed window.onload and it worked fine :)

0


source







All Articles