Flash CS5 Textfield

Posted on 2012-09-08
Last Modified: 2012-09-08
The following code creates a textfield and imports text from a text document to fill the textfield.
Then, when the playhead hits frame 30, it triggers the 2nd part of the code to replace the text.
All that works.

However, when I navigate back to frame 1, it's creating a new textfield on top of the old one (which is what my code tells it to do).
However, I just want it to create the textfield once, then on all subsequent visits to frame 1, just replace the text like it does in frame 30.

Any ideas?

Start AS3==============================
var TextURL:String;

//Setup Text Box to load external text file
var myTextLoader:URLLoader = new URLLoader();
var myText:TextField = new TextField();

TextURL = "text/introduction.txt";
myTextLoader.load(new URLRequest(TextURL));
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e:Event):void
      myText.text =;


TextURL = "text/texture.txt";
myTextLoader.load(new URLRequest(TextURL));

END AS3==========================
Question by:tlengnick
    LVL 6

    Assisted Solution

    Not really sure what all you have your code doing in the grand scheme of things, but I would suggest running an if statement to check if the textfield exists already to then just set the textfield text to what you want.

    What is happening is that every time you enter frame one, the code is run again. You can either do as I stated above or destroy the textfield in a subsequent frame; which I do not really suggest as you will be creating additional overhead with the destroying and recreating of the object.

    Author Comment

    Hey g3nu1n3, thanks for the speedy response.

    I ended up doing just what you suggested (see code below). Is there anything I could do differently that would be "cleaner" (less overhead)?

    START AS3================

    //Format Text
    var myFormat:TextFormat = new TextFormat();
    if (myText)
          trace("Doing nothing because there's already a textfield");
          var myText:TextField = new TextField();
          myText.visible = false;
          mySb.visible = false;
          txtBG_mc_i.visible = false;
    LVL 37

    Accepted Solution

    The problem with looping the frames is that all the codes in that frame gets executed meaning that onLoaded will fire everytime it loops.  It probably is not a big deal since it will load from the cache the second time, but you might have some other problems like resetting variables and stuff.

    What I would do is to create a function that takes care of everything that needs to happen only once for example initializing variables and stuff and have that function run only once by setting flags.

    The simplest example would be something along this line.
    // frame 1
    var isLoaded:Boolean;
    if (isLoaded == true)
    	trace("it is loaded already.");
    	trace("first time running.");

    Open in new window

    // frame 100
    isLoaded = true;

    Open in new window

    Since there is a variable that gets set only at frame 100, that initialization function will run only once.


    Author Closing Comment

    Thanks, CyanBlue - not sure why I didn't think of a function that initializes everything at once...still learning, I guess.

    Thanks to both of you - very helpful!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
    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…
    The goal of the tutorial is to teach the user how to how to record live broadcast.
    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.

    760 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

    11 Experts available now in Live!

    Get 1:1 Help Now