Phaser Typescript函数无法启动

时间:2016-08-15 12:34:23

标签: html5 typescript phaser-framework

我正在尝试使用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

我仍然不知道出了什么问题......

1 个答案:

答案 0 :(得分:2)

实际上我建议使用Phaser.State,这会让事情变得更容易一些。然后,您可以在TypeScript中使用extends关键字,如下所示:

class SimpleGame extends Phaser.State {

    // define your properties for SimpleGame
    coins: Phaser.Group;
    //game: Phaser.Game; // <- not needed! SimpleGame extends Phaser.State
    map: Phaser.Tilemap;
    // etc

    // define methods for SimpleGame
    create() {
        //.. create code here
        this.coins = this.add.group();

        // 'this' extends type Phaser.State, so notice you can use this.time:
        this.time.events.repeat(Phaser.Timer.SECOND * 2, 10, this.createCoin, this);
    }

    update() {
        //.. update code here
    }

    createCoin() {
        //.. create a coin
        var newCoin = new Banana(this, this.world.randomX, 0)
        this.coins.add(newCoin)
    }
}