移相器打字稿功能无法启动

哈德里安·德尔芬(Hadrien Delphin)

我正在尝试使用Phaser.io制作一些HTM5游戏。

我有一个无法解决的小问题。

在我的MainClass(游戏)中,我只想创建一个循环,该循环将创建一个新对象(硬币)并将其添加到组中。

但是我不知道为什么,我所谓的小功能永远不会启动。

这是我的代码:

  create() {

    //coins group
    this.coins = this.game.add.group;



    //set background
    this.game.stage.backgroundColor = "#00F6FA";
    //load the map
    this.map = this.game.add.tilemap('level', 195, 195, 2, 4);
    this.map.addTilesetImage('free-2d-game-tiles-post-pic-1', 'tiles');
    //load collision layer
    this.layer = this.map.createLayer('collision');
    //this.layer.debug = true;
    //make the layer collide  
    this.map.setCollisionBetween(8, 9, true, this.layer.index, true);
    //enable physics
    this.game.physics.startSystem(Phaser.Physics.ARCADE);
    //set the player
    this.player = new Player(this.game, 0, 0);


    this.game.time.events.repeat(Phaser.Timer.SECOND * 2, 10, this.createCoin, this);
}

在后面:

    createCoin(): void {
    /*

    generate a coin

    */
    console.log('test');

}

很简单,但是什么也不会发生。

你看到我想念的吗?

编辑1:

好的,这是我在Github上的代码:

https://github.com/c4n4r/coinFall/blob/master/JS/app.ts

我还是不知道怎么了...

雅各布·克罗尔(Jakub Krol)

很少有窃贼。首先,我们需要完整的代码:如何创建游戏以及如何将创建功能应用于游戏?其次,this是动态绑定的。要将其绑定到您的对象,例如,您可以使用胖箭头语法:createCoin = () => { ... }查看您的thisconsole.log(this)在create方法中)-这不是您期望的那样:)

看一下工作示例:

export default class SimpleGame {
    private game;

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

    //Used by Phaser to create real game - prepare it. Use fat arrow syntax here!
    create = () => {
        this.someInternalMethod();
    }

    //This is ok to make it as normal method, as only bounded this ( create) use it
    private someInternalMethod(){
        //Ok!
    }
}

看一下我的源代码(TS和Phaser中的简单游戏)以了解更多信息:https : //github.com/Ami777/AmyInSpace

有关TS中的胖箭头的更多信息:https : //basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

有关ES6中此语法的更多信息:https ://www.sitepoint.com/es6-arrow-functions-new-fat-concise-syntax-javascript/

此致,JakubKról。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章