Jump to content

Questions on callbacks


Recommended Posts

I realize this is more a of a typescript question than phaser, but maybe someone here can lead me on the right track.


I basically followed the making your first phaser game tutorial and finished it in typescript (typescript is another thing i have been wanted to learn for a couple months).


The real question is on callbacks (more specifically, on the overlap callback). I wasn't able to make the callback work using the following approach:

collectStars(player: Phaser.Sprite, star: Phaser.Sprite): void {    star.kill();    //  Add and update the score    this.score += 10;    this.scoreText.content = 'Score: ' + this.score;}update(){    //....    this.game.physics.overlap(this.player, this.stars, this.collectStars, null, this);    //...}

I had to make make the funcion inline for it to work :

update(){    //...    this.game.physics.overlap(this.player, this.stars,            (player: Phaser.Sprite, star: Phaser.Sprite) => {                star.kill();                this.score += 10;                console.log(this.score);                this.scoreText.content = 'Score: ' + this.score;            }            , null, this);    //...}

Now, this did work, except for the fact that whenever a star is collected the score gives me a NaN.

How should the collectStars method be declared in order for the first approach to work?


Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...