【问题标题】:Launch Phaser game启动 Phaser 游戏
【发布时间】:2016-02-21 00:20:02
【问题描述】:

我是 javascript/typescript 的新手,我正在尝试在使用 Phaser 构建一个简单的游戏时学习它。我一直在启动游戏实例:当我在 Phaser 网站上的第一个示例中创建它时,它会启动,但如果我在表单提交上调用的函数内创建游戏对象,它会启动但立即结束;这是代码:

index.html

<form class="well" onsubmit="submitForm()">
    ...
    ...
    <button type="submit" class="btn btn-primary">Play</button>
</form>

app.ts

var game: SimpleGame;

function submitForm() {
    console.log('Form submitted');
    game = new SimpleGame();
}

game.ts

class SimpleGame {

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

就像每次函数submitForm()返回时游戏对象都被销毁了。

【问题讨论】:

  • 你能在 plnkr 或 jsfiddle 上重现错误吗?

标签: javascript typescript phaser-framework


【解决方案1】:

问题在于表单提交。如果您在选择的浏览器中调出开发人员工具,您会看到表单正在有效地 POST 回页面,从而导致刷新。

一种解决方案是更新 index.html 中的 form 元素,如下所示:

<form class="well" onsubmit="submitForm(); return false;">
    // ...
    <button type="submit" class="btn btn-primary">Play</button>
</form>

return false; 将阻止 POST。

如果您不需要表单来实际发布任何数据,您可能会删除提交类型并在按钮本身上连接一个事件。

【讨论】:

  • 很高兴为您提供帮助!祝你使用 TypeScript 好运;因为 Phaser 而发现它后,我开始真正喜欢它。
猜你喜欢
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 2021-08-31
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多