简体   繁体   English

从另一个JS文件(不是游戏状态)Phaser JS调用函数

[英]Calling Function from another JS file(not a game state) Phaser js

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

    }
}

The above code is my MainGame state code. 上面的代码是我的MainGame状态代码。 In create I have created Timer.js object. 在创建过程中,我创建了Timer.js对象。 Now, how to call foo() which is written in Timer.js file. 现在,如何调用在Timer.js文件中编写的foo()。 Timer.js is not a state, but just a simple js file. Timer.js不是状态,而只是一个简单的js文件。

If instantiating a new variable reference of a class, you can simply call methods on the class from the variable you create. 如果实例化一个类的新变量引用,则可以简单地从创建的变量中调用该类的方法。

So in this case, to call method foo() , simply: 因此,在这种情况下,只需调用方法foo()即可:

this.timerJS.foo()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM