Link to home
Start Free TrialLog in
Avatar of chevronrod
chevronrodFlag for United States of America

asked on

AS3--How to concatenate name with for-loop index

This is my code:

["mcText" + i].text = IntroWords;

mcText1 (for example) is a MC of a dynamic text box.

I can do this: trace("mcText" + i); and I get mcText1, mcText2, etc. So it looks like it will concatenate the two.  

I get this syntax error: "expecting identifier before dot.", though, when I put it together like this: ["mcText" + i].text = IntroWords;

If, however, I just go with mcText2.text = IntroWords, it passes and works fine.

There is something wrong with trying to concatenate the name with the for-loop index while adding the .text behind it.

I'm old at AS2 but new at AS3.

Thanks for a thorough answer. (I know very little about classes, imports, etc. at this point.)
Avatar of gingermoleman
gingermoleman

Hi,

Ive always set the name value when adding the movieclips

mcText.name="mcText"+i;

and then to use it its:

getChildByName("mcText"+i).text = Introwords.toString();

GMM
Avatar of chevronrod

ASKER

Well, I am now getting these three erros. Maybe they make some sense to you. :)


1120: Access of undefined property mcText.        mcText.name = "mcText"+i;

1120: Access of undefined property Introwords.  getChildByName("mcText" + i).text = Introwords.toString();

1119: Access of possibly undefined property text through a reference with static type flash.display:DisplayObject.   getChildByName("mcText" + i).text = Introwords.toString();
Can you post the rest of the code? I need to see whats causing the text to populate (eg what "i" equals or whaat triggers it)

As to introword not being defined, have you declared this in a function somewhere else? the error basically says ity doesnt exist and this is usually when you declare it in one function and try to use it in another.

Im opposite to yourself, know as3, not as2 but im always reading about how simple as2 things are now more complex.


GMM
From the peanut gallery, regarding gingermoleman's solution, about this line:

mcText.name="mcText"+i;

I think this name assignment needs to be done at the point that each "mcText" is created, or immediately thereafter.  Either way, there probably isn't ever really a simple variable called "mcText", unless it is a local variable used in the create loop.

sjklein42 is correct on name assignment, must be done at creation of the mc. if its placed on the stage using the GUI then you can set the instanceName instead under properties.

GMM
SOLUTION
Avatar of sjklein42
sjklein42
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here it is.


//30 Words for Intro page
var IntroWords:Array = new Array("Beautiful!", "Glory!", "Hallelujah!","Wonderful!","Counselor!","Prince!","Lord!","Savior!",
      "Messiah!","The Way!","The Life!","Risen!","Loving!","Merciful!","Son!","Patient!","Kind!","Royal!","Human!","Rabbi!","Caring!",
      "Mighty!","Forgiving!","Healing!","Creator!","Teacher!","Accepting!","Eternal!","Heaven!","Faithful!");

Initialize();

function Initialize():void
{
      LoadIntroWords();
      //Much more to be added here
}

function LoadIntroWords()
{
      for (var i = 1; i <= 6;i++)
      {
            mcText.name="mcText"+i;

            getChildByName("mcText"+i).text = Introwords.toString();

      }
}
Ok, from what your doing here, forget getChildByName.

does mcText exist in the library and marked with export?

if so your code should be something like

 for (var i = 1; i <= 6;i++)
      {
           var mcText:mcText = new mcText;
            mcText.name="mcText"+i;
            mcText.<NAMEOFTHETXTFIELD>.text = introWords(somehow!!);
      }

I say somehow with the intro words, do you want 6 at random? Sorry to be a pain but any chance of uploading the FLA?
oops, the var line should read:

var mcText:mcText = new mcText();
Here's the .fla.

Am getting only one error now.

This is very basic and I changed the IntroWords to just one specific one to eliminate any more confusion. If we can get this right I can build the rest from here.
LNE.fla
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm providing this final version of the code fix, as well, for others that might need it:

            // THIS LINE DEFINES mc1Text as tempMc
            var tempMc:Object = getChildByName("mc"+i+"Text");
            // this line uses tempMc (which is another way now of saying mc1Text) to change th text
            tempMc.dText.text = IntroWords[0];