Solved

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

Posted on 2011-02-20
12
1,379 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
And the other line probably doesn't need the .toString()

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

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:chevronrod
Comment Utility
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
Comment Utility
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
Comment Utility
oops, the var line should read:

var mcText:mcText = new mcText();
0
 

Author Comment

by:chevronrod
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
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. …
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

771 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

12 Experts available now in Live!

Get 1:1 Help Now