Solved

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

Posted on 2011-02-20
12
1,390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
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 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…

627 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