【问题标题】:Calling Function from another JS file(not a game state) Phaser js从另一个 JS 文件(不是游戏状态)调用函数 Phaser js
【发布时间】:2018-08-16 06:47:27
【问题描述】:
import Timer from '../components/Timer.js';

export default class MainGame extends Phaser.State {
    preload() {
        console.log("create");
    }
    create() {
        this.timerJS = new Timer(this.game);
    }
    update() {

    }
}

以上代码是我的MainGame状态代码。在创建中,我创建了 Timer.js 对象。现在,如何调用写在 Timer.js 文件中的 foo()。 Timer.js 不是状态,只是一个简单的 js 文件。

【问题讨论】:

  • 你能显示Timer.js文件吗?
  • 导出默认类 Timer extends Phaser.Game { create() { console.log("timer",this); } foo() { console.log("new"); } }
  • 你试过this.timerJS.foo()吗?
  • 成功了。谢谢。 :)
  • 也添加为未来读者的答案

标签: javascript ecmascript-6 phaser-framework es6-class


【解决方案1】:

如果实例化一个类的新变量引用,您可以简单地从您创建的变量调用该类的方法。

所以在这种情况下,要调用方法foo(),只需:

this.timerJS.foo()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2018-03-20
    相关资源
    最近更新 更多