Copy link to clipboard
Copied
Hi there,
I got a button on the stage and a variable set to 0.
I would like to increase the value of the variable every time the user clicks on the button.
I try this code but not sure about the function...
//dtc_Hits is the instance name of the dynamic text field;
//inst_ButtonMovie is the instance name of the button to be clicked
var Hits = 0;
this.dtc_Hits.text = Hits
this.inst_ButtonMovie.on("click", function(increaseHits) {
Hits++;
})
PS. I know that the error is in the line Hits++ because I try an alert message and it works...
Yes, your Hits++ works, but you need to update the text field after you update the variable. So just add the line this.dtc_Hits.text = Hits; as the last line in your function.
Copy link to clipboard
Copied
Yes, your Hits++ works, but you need to update the text field after you update the variable. So just add the line this.dtc_Hits.text = Hits; as the last line in your function.
Copy link to clipboard
Copied
Thanks so much. I knew it is a small thing ...
It works well, but the counter resets when the movie loops. it is about 4 seconds - 100 frames.
How could I make it so that the variable continues to be increased even after the movie loops?
Thanks again!
Copy link to clipboard
Copied
That’s because your loop goes through the frame where you set the variable’s initial value. So, if all of this code is on frame 1, just loop back to frame 2 and the variable won’t be reset to 0.
Copy link to clipboard
Copied
Thanks, that did the trick.
Now I need to dig deeper and set a score as an objective.
Copy link to clipboard
Copied
I’m glad that I could help. Be sure to start a new thread for each new question when they come up.