• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

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;
      addChild(myText);
      addChild(mySb);
}

//THE FOLLOWING CODE SITS ON FRAME 30

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

END AS3==========================
0
tlengnick
Asked:
tlengnick
  • 2
2 Solutions
 
g3nu1n3Commented:
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.
0
 
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");
}
      else
      {
      var myText:TextField = new TextField();
      myText.visible = false;
      mySb.visible = false;
      txtBG_mc_i.visible = false;
      }
0
 
CyanBlueCommented:
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.");
}
else
{
	trace("first time running.");
	yourInitializationFunctionShouldRunHere();
}

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.

CyanBlue
0
 
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!
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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now