Calculate score
In this section, you will learn how to add a function that increases the score when a player removes bubbles. We already have the removeKilledBubbles()
function, which removes killed bubbles from the stage. So, we can add our score calculation and display logic here as well.
bubbles.forEach(function(bubble) { if (!bubble.alive) { setBubblePos(bubble, -1,-1); score++; } }); scoreText.text = 'score: ' + score;
Here, you can see that we incremented the score
global variable and updated the text of the scoreText
object. Now, you can refresh the browser, and as you remove each bubble, you'll see the score increase by 1.