Flash CS5 Textfield

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 = e.target.data;


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

END AS3==========================
Who is Participating?
CyanBlueConnect With a Mentor Commented:
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.

g3nu1n3Connect With a Mentor Commented:
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.
tlengnickAuthor Commented:
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;
tlengnickAuthor Commented:
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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.