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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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