Unable to "record" score


I need help again!

Link to the project or level: https://studio.code.org/projects/applab/frGJYjvYCobxBK9B1PjtaOBqjpVt6brf0-K2HiLfBkA

What I expect to happen: When the basketball is clicked text will appear at the top of the app showing how many balls I have collected (score) and how many lives I have left, if I have clicked the background.

What actually happens:
Right now, I have no count on the basketball no matter how many times I click the ball, the lives count down, but there is no score.
What I’ve tried: I have tried taking out the setText to hopefully create a counter on the basketball, but nothing is happening. I tried putting the code back into the program and am still not getting the results I was hoping to gain.

Here’s the code:

var score = 0;
var lives = 3;
onEvent(“start_button”, “click”, function() {
onEvent(“basketball”, “click”, function() {
score = (score+1);
setPosition(“basketball”, randomNumber(200, 10), randomNumber(200, 10));
if (score == 10){

lives = (lives-1);
setText(“number_lives”, lives);
if (lives==0){
onEvent(“tryAgain_button”, “click”, function(){
onEvent(“playAgain_button”, “click”, function() {

Thank you for any help you may be able to offer.

Congratulations on getting your code this far. I see only a minor problem which is not uncommon. Your text labels are named “countDisplayLabel” and “countDisplayLabel2” and not “total_score” and “number_lives”. You will need to go to design mode and rename your labels to “total_score” and “number_lives”. Then you need to put back the setText in the on event that checks if the basketball is clicked.

1 Like

As above.

setText() moves a value from a variable like score or lives into a GUI element on your screen like a button or label and only does that when called. That means you call setText() every time the variable changes value. There is no permanent connection between variable and GUI element.

It also looks like you have two event handlers for the “tryAgain_button”.

You will also need to reset the score and lives back to 0 and 3 when you start the game over.

When looking for graphics on the web like the basketball look for a .png with transparency. That will eliminate the white box around the basketball. .jpg images don’t have an alpha channel so you always get the box around the image with them unless the color matches your background.