VSCode 未读取打字稿的 Phaser 定义

Phaser Definitions for typescript not being read by VSCode

我一直在关注 this tutorial 的相位器,但我无法 运行 第 4 项中的代码。我完全复制了它,没有任何修改(见下文,如果你还没有看过教程),但我 运行 犯了错误。

new Phaser.Game(800, 600, Phaser.AUTO, 'content', { preload: this.preload, create: this.create }); 给我错误消息“[ts] 预期有 0​​-1 个参数,得到 5 个”,我还被告知属性 "load," "add" 和 "world" 对于类型 "Game."

不存在

尝试编译时,我得到了这个响应。

    TSError: ⨯ Unable to compile TypeScript:
app.ts(4,25): error TS2304: Cannot find name 'Phaser'.
app.ts(4,47): error TS2304: Cannot find name 'Phaser'.
app.ts(7,11): error TS2503: Cannot find namespace 'Phaser'.

    at createTSError (C:\Users\Username\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:261:12)
    at getOutput (C:\Users\Username\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:367:40)
    at Object.compile (C:\Users\Username\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:558:11)
    at Module.m._compile (C:\Users\Username\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:439:43)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\Username\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:442:12)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)

我现在的app.ts:

class SimpleGame {

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

    game: Phaser.Game;

    preload() {
        this.game.load.image('logo', 'phaser2.png');
    }

    create() {
        var logo = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'logo');
        logo.anchor.setTo(0.5, 0.5);
    }

}

window.onload = () => {

    var game = new SimpleGame();

};

如有任何帮助,我们将不胜感激。谢谢!

尝试添加对您的定义文件的引用。将此放在 app.ts

的开头
/// <reference path="path/to/your/deffile/phaser.d.ts" />

此外,不要忘记添加 Pixi 定义。

看起来 TypeScript 教程中的示例代码对于 Phaser 3 已过时。参见 the current example JavaScript code. The current TypeScript tutorial 也已过时。考虑使用 Phaser 的支持渠道之一来报告问题。同时,您可以将您的 TypeScript 代码基于 JavaScript 示例。