Dynamic Instance Names of TextFields - A work around

07 March 2007

As I am starting to get back into heavy Flash development after my 5+ year hiatus, I see that Macromedia has added quite a lot of interesting concepts to the newer versions of ActionScript.

But the purpose of this post is to outline that Macromedia (err now Adobe) still has a lot of work to do on their ActionScript engine, specifically with Dynamic Instance naming conventions (and new-object intellisense would really be nice by now, after all of these years).  I am assuming you know flash, a little bit.  At least to the level of knowing that you need set an InstanceName of a Symbol, in order to be able to edit its properties at runtime. 

I just spent too much time today trying to figure out why my code wasn't working.  I am creating numerous dynamic TextFields, but I need to be able to access them for updates in code.  And to complicate things, the TextFields will be loaded from different XML files - meaning they will have different InstanceNames.

You usually can create a dynamic instance name by using the old-school method of hardCodedName[variable] or the even older eval("hardCodedName" + variable) method.  For example, say I want to create a new movie into a dynamic variable made up of an integer I have in a for i++ loop:

for (int i = 0; i < total; i++)
{
  loadMovie( "myMovie" + i + ".swf", _root.myMovie[ i ] );
}

Or...

for (int i = 0; i < total; i++)
{
  loadMovie( "myMovie" + i + ".swf", _root.eval("myMovie" + i) );
}

Both of these examples should result in loading myMovie0.swf into the dynamic variable name of /level0/myMovie0 (or _root.myMovie0 per dot-syntax). 

What I found today is that when using some of the newer ActionScript 2.0 and 3.0 functions, they do not honor these methods completely.  I had to use a mixture of the two examples above to get it work, and actually could not use either method when creating the object!  This is the craziest thing I've seen to where eval() works in referencing the object, but does not work in the creation of the object. 

var _root.totalCount:Number = 0;
function CreateMenuItem(Text:String, Href:String, Title:String, Spacer:Boolean)
{
    // create the symbol at the last Y position we are tracking
    this.createTextField("movFlyoutText" + _root.totalCount
        , _root.totalCount, _root.textXpos, _root.lastYpos, _root.textWidth, _root.textHeight);
    eval("movFlyoutText" + _root.totalCount).wordWrap = true;
    eval("movFlyoutText" + _root.totalCount).multiline = true;
    eval("movFlyoutText" + _root.totalCount).border = false;
    eval("movFlyoutText" + _root.totalCount).type = "dynamic";
    eval("movFlyoutText" + _root.totalCount).antiAliasType = "advanced";
    
    textFormat = new TextFormat();
    textFormat.color = 0xffffff;
    textFormat.size = 12;
    textFormat.font = "Futura Condensed";
    
    if (Spacer != true)
    {
      formatText.url = Href;
    }
    
    if (Text.length > 0)
    {
        eval("movFlyoutText" +  _root.totalCount).text = Text;
    }
    else
    {
        eval("movFlyoutText" + _root.totalCount).text = " ";
    }
    
    eval("movFlyoutText" + _root.totalCount).setTextFormat(textFormat);
    
    // reset some vars
    var tmp:Number = eval("movFlyoutText" + _root.totalCount)._height;
    _root.lastYpos = _root.lastYpos + tmp;
    
    _root.totalCount++;
}

Above is the code I wrote today, to where I can not use [] reference or the eval() reference to get the TextField created properly using the newer createTextFieldmethod.  But in other methods throughout my code, using the [] reference works at the baseline.  Go figure.

And as mentioned above, I can not use brackets[] when referencing the object either to set its properties!  I had to use eval().  Man, how ugly is this code?  I'm open to other suggestions.

 
Reader's Comments
 
09 March 07 2:54 PM

Ok, I figured out with some AS 2.0 variables a way to get around the eval() methods you see above.  Putting two and two together, I came up with defining the TextField as a new object, and referencing the eval() function.  It worked!

var newText:TextField = eval("movFlyoutText" + _root.totalCount);

And now I can access the movFlyoutText[#] instance such as setting the wordWrap to true like this:

newText.wordWrap = true;

Please note that you still must define the TextField instance as I do above in the original post dynamically, before you can reference it like this. 

AS 2.0 seems to be an attempt to conform to a standing programming model, but still sorely lacks in many fundimentals. 

Macromedia (ok, now Adobe), remember me?  The guy you tried to sue-than-hire for my previous work with Flash4 down the street from your offices in NYC?  :)  Well, I have a suggestion for you guys.  Dump the cross-breeding of ActionScript versions and just make a new editor with no compatibility.  For example, moving from Visual Studio 6.0 to Visual Studio 2003 or 2005?  Yeah, completely new languages and formats.  And it worked out greatly!

Leave a Comment
Comment Policy: HTML is not allowed. Links and line breaks are converted automatically.
(required) 
(optional)
(required) 

 


  
Enter the anti-spam code you see above (required)

 

Comment Notifications
Subscribe to this post's comments using RSS

If you would like to receive an email when updates are made to this post, please register here