【问题标题】:Phaser Definitions for typescript not being read by VSCodeVSCode 未读取 typescript 的 Phaser 定义
【发布时间】:2019-02-04 18:18:03
【问题描述】:

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

new Phaser.Game(800, 600, Phaser.AUTO, 'content', { preload: this.preload, create: this.create }); 行给了我错误“[ts] 预期 0-1 参数,得到 5”,而且我还被告知属性“加载”、“添加”和“世界”不t 存在于“游戏”类型中。

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

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

};

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: typescript visual-studio-code phaser-framework


    【解决方案1】:

    TypeScript 教程中的示例代码对于 Phaser 3 来说似乎已经过时了。请参阅 the current example JavaScript codeThe current TypeScript tutorial 也已过时。考虑使用 Phaser 的支持渠道之一来报告问题。同时,您可以将您的 TypeScript 代码基于 JavaScript 示例。

    【讨论】:

      【解决方案2】:

      尝试添加对您的定义文件的引用。把它放在你的 app.ts 的开头

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

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

      【讨论】:

        猜你喜欢
        • 2016-05-07
        • 2020-07-27
        • 2018-11-06
        • 2022-01-22
        • 1970-01-01
        • 2021-11-20
        • 2018-03-13
        • 2018-08-30
        • 1970-01-01
        相关资源
        最近更新 更多