A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: how to make a simple dynamic text field

  1. #1
    Senior Member
    Join Date
    May 2005
    Location
    Canada
    Posts
    212

    how to make a simple dynamic text field

    all i want to do is have a dynamic text box that loads text from .txt file. I can't find a simple tutorial on this. I know i make a text box, give it an istance name say "holder" but i dont know what else. help please

  2. #2
    Registered User greenham's Avatar
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    555

    Loading text from an external .txt file

    Ok, TeddTucker. This is something I had quite a bit of trouble with when I was starting out with Flash. Shamefully, I had to go back and look it up again today, but it's nothing too troublesome. I'll walk you through it.

    First of all, create a new layer, name it 'textbox' and place a dynamic text box on it with the desired demensions. Now, give it an instance name - I'm going to use 'txt_box'.

    Now click on the first frame of the layer you just created and press f9 to open the ActionScript pannel. In it, copy and paste the following code:
    Code:
    get_text = new LoadVars();				//1
    get_text.load('text.txt'); 				//2
    
    get_text.onLoad = function(success)		//3
    	{
    		txt_box.text = get_text.txt;	//4
    	}
    In line 1 we are creating a new LoadVars object named 'get_text'. Line 2 is just instructing Flash to fetch the contents of a .txt file we are yet to create. Line 3 creates a condition for when the contents of the .txt file have been loaded. Line 4 just assigns the text contained in the .txt file to your text box, txt_box.

    Now, compile your .swf and like magic . . . nothing happens. That's ok. Navigate to the folder where you compiled the swf to, and either create a new text file by right clicking> new > Text Document, or paste one you already have.

    Open up the text file and type this (adjusting as necessary):
    Code:
    &txt = "your text";
    Notice that 'txt' variable is referred to in line 4 of your actionscript code. You can send flash more textfield values from this same text field, separating each with a '&'. For example:
    Code:
    &txt = "some text"&txt2 = "some more text";
    To refer to this new variable, you must add '.txt2' to the end of your get_text object. For example, if you had another text field name 'txt_box2' then your code would read:
    Code:
    get_text.onLoad = function(success)		
    	{
    		txt_box.text = get_text.txt;	
    		txt_box2.text = get_text2.txt;	
    	}
    I hope that explains it for you.
    Last edited by greenham; 12-03-2006 at 11:20 PM.

  3. #3
    Senior Member
    Join Date
    May 2005
    Location
    Canada
    Posts
    212
    greenham, thank you for your detailed help but i can't get it to work. i did everything u said but the text field is coming up "undefined" when i preview it. any idea why this is happening?

  4. #4
    Junior Member
    Join Date
    Nov 2006
    Posts
    3
    Hi Greenham

    I have a somewhat related problem: I have some titles on the left-hand side of a page. I want users to be able to click a title, which would then open a text box or window component, providing formatted text info about the title in the box.

    That would mean clicking the title should load the txt1/2/3/4 etc variable, so that the correct variable text is called from the text file.

    How do I do that?
    Last edited by mpandlane; 12-05-2006 at 06:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center