Skip to main content
Known Participant
September 7, 2017
Answered

updating numerical dynamic text

  • September 7, 2017
  • 1 reply
  • 983 views

Have a dynamic text field called:  rpmText.

Need to populate the field with a number that is controlled and constantly updated by the frames of a movie clip on stage.

For example, from frame 0-30 the field should read 0, but from frame 30-60, the numbers will rapidly count up to 1,000.

Could someone point me in the direction of tutorials/resources to pull this off?

    This topic has been closed for replies.
    Correct answer Colin Holgate

    You would think that adding a number to the field each frame would do it, but humans would notice that, so having it increase by a random amount would be more interest. You also want it to stop at exactly 1000.

    So, this script in frame 1 of the movieclip would repeatedly set the text in the field that is also on the stage next to the movieclip:

    createjs.Ticker.addEventListener("tick", rpm.bind(this));

    var rpmValue = 0;

    function rpm() {

      var f = this.currentFrame;

      if (f >= 30) {

      rpmValue += Math.random() * 10;

      rpmValue += 30;

      }

      this.parent.rpmText.text = Math.min(1000, Math.round(rpmValue));

    }

    It will set the value to 0 until the movieclip reaches frame 30, then it will add at least 30, plus a random amount up to 10, to the field. You could increase either number to make it get there quicker.

    1 reply

    Legend
    September 7, 2017

    Just set the .text property of the textfield to whatever you want it to display.

    Known Participant
    September 7, 2017

    I think the point is I have no idea how to do this...much less dynamically linking it to test field that continually updates on the fly.

    Colin Holgate
    Colin HolgateCorrect answer
    Inspiring
    September 7, 2017

    You would think that adding a number to the field each frame would do it, but humans would notice that, so having it increase by a random amount would be more interest. You also want it to stop at exactly 1000.

    So, this script in frame 1 of the movieclip would repeatedly set the text in the field that is also on the stage next to the movieclip:

    createjs.Ticker.addEventListener("tick", rpm.bind(this));

    var rpmValue = 0;

    function rpm() {

      var f = this.currentFrame;

      if (f >= 30) {

      rpmValue += Math.random() * 10;

      rpmValue += 30;

      }

      this.parent.rpmText.text = Math.min(1000, Math.round(rpmValue));

    }

    It will set the value to 0 until the movieclip reaches frame 30, then it will add at least 30, plus a random amount up to 10, to the field. You could increase either number to make it get there quicker.