Solved

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

Posted on 2011-02-20
12
1,381 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

778 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