• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Increase a variable in Dynamic Text field with JS

Engaged ,
Jun 05, 2018 Jun 05, 2018

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

Views

708

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 05, 2018 Jun 05, 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.

Votes

Translate

Translate
LEGEND ,
Jun 05, 2018 Jun 05, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 05, 2018 Jun 05, 2018

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2018 Jun 05, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Thanks, that did the trick.

Now I need to dig deeper and set a score as an objective.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

LATEST

I’m glad that I could help. Be sure to start a new thread for each new question when they come up.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines