Skip to main content
Inspiring
March 23, 2018
Answered

Trying to create dynamic text from a formula

  • March 23, 2018
  • 1 reply
  • 439 views

I have a number of dynamic text windows in which I want to display some text that is generated from string variables containing the desired text. I want the text to be displayed when a button is clicked. The following code shows what I am trying to do.

The variables containing text are:

var 101:String = ("TEXT FOR VARIABLE 101");

var 201:String = ("TEXT FOR VARIABLE 201");

var 301:String = ("TEXT FOR VARIABLE 301");

This is the button that makes it happen.

buttonA1_btn.addEventListener(MouseEvent.CLICK,button1_btnClick);

function button1_btnClick(event:MouseEvent):void

{

    selectA1_txt.text = (1 + youAreHere_txt.text + ("1"));

    selectA2_txt.text = (2 + youAreHere_txt.text + ("1"));

    selectA3_txt.text = (3 + youAreHere_txt.text + ("1"));

}

In the above equations the value of youAreHere_txt.text is 0 for the initial case, hence the values of the three statements are 101,201 and 301.

But I don't want to see those values. I want to see the text that those values represent, ie TEXT FOR VARIABLE 101 etc.

Is there a way of doing what I am looking for? Your help would be much appreciated.

This topic has been closed for replies.
Correct answer dougw39890993

Thanks for your answer. I think you have set me on the path to success with this program.

You correctly pointed out that variables cannot start with a number and you fixed that by preceding the number with an underscore.

I do need to get the concatenated values to form the variable names. Your suggestion worked after I moved some code so that the concatenated statement preceded the statements containing the values that are used in the concatenated formula. I ended up with the following code.

function button1_btnClick(event:MouseEvent):void

{

    selectA1_txt.text = this["_1" + youAreHere_txt.text + "1"];

    selectA2_txt.text = this["_2" + youAreHere_txt.text + "1"];

    selectA3_txt.text = this["_3" + youAreHere_txt.text + "1"];

}

var _101:String = "TEXT FOR VARIABLE _101";

var _201:String = "TEXT FOR VARIABLE _201";

var _301:String = "TEXT FOR VARIABLE _301";

So, thanks again for your answer. It was very helpful.

1 reply

JoãoCésar17023019
Community Expert
Community Expert
March 23, 2018

Hi.

There are some things that need to be clarified:

- You can't start a variable name with numbers. You can only start a variable name with letters, _ and $.

- You are trying to concatenate values to form your variable names while what I guess you really want/need is just to assign the strings that your three variables hold to the text property of your text fields;

Try this:

var _101:String = "TEXT FOR VARIABLE 101";

var _201:String = "TEXT FOR VARIABLE 201";

var _301:String = "TEXT FOR VARIABLE 301";

function button1_btnClick(event:MouseEvent):void

{

    selectA1_txt.text = _101;

    selectA2_txt.text = _201;

    selectA3_txt.text = _301;

   

    // but if you really want to get the values forming the variable names, uncomment the following lines and comment out the previous ones.

    //selectA1_txt.text = this["_1" + youAreHere_txt.text + "1"];

    //selectA2_txt.text = this["_2" + youAreHere_txt.text + "1"];

    //selectA3_txt.text = this["_3" + youAreHere_txt.text + "1"];

}

buttonA1_btn.addEventListener(MouseEvent.CLICK, button1_btnClick);

I hope it helps.

Regards,

JC

dougw39890993AuthorCorrect answer
Inspiring
March 24, 2018

Thanks for your answer. I think you have set me on the path to success with this program.

You correctly pointed out that variables cannot start with a number and you fixed that by preceding the number with an underscore.

I do need to get the concatenated values to form the variable names. Your suggestion worked after I moved some code so that the concatenated statement preceded the statements containing the values that are used in the concatenated formula. I ended up with the following code.

function button1_btnClick(event:MouseEvent):void

{

    selectA1_txt.text = this["_1" + youAreHere_txt.text + "1"];

    selectA2_txt.text = this["_2" + youAreHere_txt.text + "1"];

    selectA3_txt.text = this["_3" + youAreHere_txt.text + "1"];

}

var _101:String = "TEXT FOR VARIABLE _101";

var _201:String = "TEXT FOR VARIABLE _201";

var _301:String = "TEXT FOR VARIABLE _301";

So, thanks again for your answer. It was very helpful.