Solved

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

Posted on 2011-02-20
12
1,380 Views
Last Modified: 2012-05-11
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.)
0
Comment
Question by:chevronrod
  • 6
  • 4
  • 2
12 Comments
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34939376
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
0
 

Author Comment

by:chevronrod
ID: 34939525
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();
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34939556
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
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34939582
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.

0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34939605
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
0
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 50 total points
ID: 34939612
And the other line probably doesn't need the .toString()

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

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:chevronrod
ID: 34939635
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();

      }
}
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34939948
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?
0
 
LVL 5

Expert Comment

by:gingermoleman
ID: 34939962
oops, the var line should read:

var mcText:mcText = new mcText();
0
 

Author Comment

by:chevronrod
ID: 34940044
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
0
 
LVL 5

Accepted Solution

by:
gingermoleman earned 450 total points
ID: 34940123
Ok, I see your problem.

your mcText is an object you have "placed" on the stage via the gui. You need to click on it and set the instance name that you can refer to in the AS3. ( i have done this and "fixed" the code)
if you want 6 of these then you'll need to add another 5, giving all of them a unique name. HAving numbers at the start of these names is a no no, I tend to capture these in the middle.

For the loop to work, you just need to define the object first before you start adjusting it. Again, this is in the FLA

GMM
LNE.fla
0
 

Author Closing Comment

by:chevronrod
ID: 34942977
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];
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to record live broadcast.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now