打印稿窗口.Onload不被调用后,增加了相位游戏的要求

Typescript window.onload not being called after adding requirejs for phaser game

本文关键字:游戏 增加 Onload 窗口 调用 打印稿      更新时间:2023-09-26

我正在尝试使用相位器和Typescript制作游戏。我按照这里的说明做了,它最初起作用了。问题来了,当我试图使用AMD模块化我的代码和要求

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();
};

现在当我加载它的窗口。onload从来没有被调用过,我试着遵循窗口。Onload不调用函数对于Javascript解决方案,但我不能让它在typescript中工作。

这里是生成的Javascript如果你想看一下https://gist.github.com/koreus7/06bee4a30d22bdc76d62

的Javascript解决方案,但我不能让它在typescript工作。

基本上,如果窗口已经加载到window.onload,将不会调用附加的函数。

检查document.readyState === "complete"。或者使用jquery ready: https://api.jquery.com/ready/

我相信这是由require.js加载器引起的,它没有我明确地知道它有点类似于jspm system.js,所以我们不需要确保窗口被加载,因为加载器已经确保了。

我刚把窗户拆了。Onload和它工作得很好:)