Jump to content

Score update


KnTproject
 Share

Recommended Posts

Hello,

 

I want to add a score to my phaser game. My game is a road with a lot of cars that you have to avoid. My game goes from top of the screen (-400) to down (200). Now I have that if the car is from the screen, it has to kill the actual car. And if that car is killed, then the score +1. 

My car is this.auto

This is my code in the create function:
this.score = 0;
    this.labelScore = game.add.text(20, 20, "0", 
        { font: "30px Arial", fill: "#ffffff" });  

This is my code in the update function:
 if(this.auto.body.velocity.y  > 300){
        this.auto.kill();
        addScore();
    }

This is my code in de addScore function:
 addScore:function(){
        if(this.auto.kill() == true){
            this.labelScore.text = this.score; 
            this.score += 1;
                    }    },

 

But it doesn't work. I don't know if its the kill of the text that doesn't work. Does anybody knows what the problem is?

 

Link to comment
Share on other sites

You can check if a certain function is called by adding a console.log and check your browser console.

Anyway, what I always use for updating text is the following:

labelScore.setText(score);

Never used <object>.text so not sure if that would matter, but you can try I guess.

I would also recommend to change your score before changing your text, otherwise it will always be 1 behind :).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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