How do you create a counter using variables?

I’m making a clicker game. I need help

1 Like

Declare your score tracking variable.

var score =0;

Then, inside the button press code, increase the
value of score.
score +=1;

Here’s a quick example that should help!
cookieClicker.wick (1.8 KB)

Also, you may want to look at the button and interactive text tutorial video!

The general idea is

  1. Create a button that can be clicked
  2. Create a variable in the project that we can store a number in.
  3. Increase that number everytime a click happens!

After we do this, we can use those points to create a purchasing system, prizes, etc… Let us know if you’d like help at that step!

2 Likes

Thanks for the help! :smiley:

1 Like

Hello, I was trying to creat a quizz-like game and using your example to keep score.
As soon as I created a tween with the clickable buttons, I started getting an error of unknown “scoreCounter”.
Is it an object scope problem?

Hi @Blaylock, when you tween a layer, all of the objects (clips, buttons, text, paths, etc.) inside the layer are placed into a new unnamed clip. Let’s say one one of the objects is called “button”, then in order to access “button” from the project timeline you’ll have to name the unnamed clip, lets say “tween”, and then from a script you can write tween.button, so yeah it’s probably a scope problem :+1:

I might have misunderstood the question, let me know if so!

My issue is the other way around, ie, I have a textbox1 on layer1 and a bunch of buttons in layer2.
When I create a tween for the buttons on layer2 the code in them stops “recognising” textbox1.

How can I fix this?

1 Like

Ah yeah that makes sense. The way I get around that issue is in the first frame of the project I put
project.textObject = textObject
And then from inside the tween with all the buttons you just replace references to textObject with project.textObject

3 Likes

Thanks for the video mate! I made a clicker counter now I am looking to add a functionality where after every 100 clicks, the user gets an option to get another button for clicking. How should I approach this? Please guide.