Skip to main content
Inspiring
June 5, 2018
Answered

Increase a variable in Dynamic Text field with JS

  • June 5, 2018
  • 1 reply
  • 1049 views

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...

This topic has been closed for replies.
Correct answer robdillon

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.

1 reply

robdillon
robdillonCorrect answer
Participating Frequently
June 5, 2018

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.

Inspiring
June 5, 2018

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!

robdillon
Participating Frequently
June 5, 2018

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.