Results 1 to 6 of 6

Thread: Import text from file WITHOUT LoadVars AS2

  1. #1
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Import text from file WITHOUT LoadVars AS2

    I wanted to see if anyone knew a way I could accomplish this.

    I have a flash file made in AS 2.0 (I don't want to change it to AS3 and risk breaking its current code). I want to be able to load text to display in a TextArea component.

    I know I can load the text using the LoadVars class, but that requires some odd formatting. The text file has to contain the name of the variable as well as store the entire string in one line. I want to be able to use the HTML as a separate file or at least be able to look at it in an editable way.

    Is there any way I can just read all of the data inside a text file as one long string? I should be able to use AS to remove any newlines then and display it as the text for my TextArea component. What do you all think?

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Import text from file WITHOUT LoadVars AS2
    From a basic .txt file, I'm afraid there is no way to do it. XML is another option. That's still using the loadVars class but it's much more flexible than simple text files. There are also PHP-specific ways to communicated with PHP. But with text files, loadVars is your only option.

    I have a flash file made in AS 2.0 (I don't want to change it to AS3 and risk breaking its current code).
    There is no reason to switch to AS3 unless you require the new functionality in it. AS2 works just fine for the time being. If the flash player stops supporting AS2, then you'll have to start worrying. But I don't see that happening for a while.

    I could be wrong though...with AS4 already in the works. But from what I hear the CS4 release of Flash will not support AS4, so it might be a long time from now.

    The text file has to contain the name of the variable as well as store the entire string in one line.
    You don't necessarily need the variable. The only time it would be useful is if you're adding associated data -- an image with it's caption, for example. But even then there are ways around it.

    Also, it certainly doesn't have to be on one line.

    If you're up for a ridiciously overcomplicated read, the "new to flash mx 2004" topic below this one shows an example of this. Try the last post on page 10.

    Is there any way I can just read all of the data inside a text file as one long string? I should be able to use AS to remove any newlines then and display it as the text for my TextArea component. What do you all think?
    Sure, are you familiar with regex? There are a few ways this can be done.

    I'm a little confused by what you need here, though. You'd like the text file to not have everything on one line but you would like Flash to output it as such? It would help if you could paste a snippet of what you want your text file formatted as and a snippet of what the textarea should look like.

  3. #3
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i guess the formatting assumptions are a bit of a misconception on my part. thanks for clearing this up.

    i'll show you an example of what I am trying to do.

    First i have a basic HTML file (faq.html):

    Code:
    <html>
    <head><title>Frequently Asked Questions</title></head>
    <body>
    <h1>This is the first question?</h1>
    <p>This is the first answer.</p>
    <h1>This is the second question?</h1>
    <p>This is the second answer.</p>
    <h1>This is the third question?</h1>
    <p>This is the third answer.</p>
    </body>
    </html>
    I want it to appear inside a TextArea component instance with formatting (a whole diff beast but maybe you can help me with this too). So i have the following AS on frame 1 of a movieclip with an instance of the component on the stage that has an instance name "textarea"

    Code:
    //var textarea:mx.controls.TextArea;
    
    textarea.depthChild0._alpha = 0;
    
    // Create the new StyleSheet object.
    var my_styles:TextField.StyleSheet = new TextField.StyleSheet();
    my_styles.setStyle("h1", {fontFamily:"Arial,Helvetica,sans-serif", color:"#FF0000", fontWeight:"bold"});
    my_styles.setStyle("p", {fontFamily:"Arial,Helvetica,sans-serif", color:"#000000"});
    
    // Set the TextAreaInstance.styleSheet property to the newly defined
    // styleSheet object named styles.
    textarea.styleSheet = my_styles;
    textarea.html = true;
    
    // Load text to display and define onLoad handler.
    var my_lv:LoadVars = new LoadVars();
    my_lv.onData = function(src:String) {
     if (src != undefined) {
      textarea.text = src;
     } else {
      textarea.text = "Error loading HTML document.";
     }
    };
    my_lv.load("faq.html");
    i think this should produce what i want, but i end up with something that looks like this:

    Code:
    Frequently Asked QuestionsThis is the first question?This is the first answer.
    This is the second question?This is the second answer.
    This is the third question?This is the third answer.
    for reference, i want it to look like this:

    This is the first question?
    This is the first answer.
    This is the second question?
    This is the second answer.
    This is the third question?
    This is the third answer.

    (notice that I dont want the title to appear, i want line breaks between the lines of text, and i want color and font-weight styling)

  4. #4
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    You're kind of over-complicating things here.

    I seem to be saying this a lot in these forums lately, but Flash isn't designed to be a HTML browser. Full flash websites aren't designed by parsing HTML markup. It's not recommended for various reasons...inflexibility being a huge one.

    Flash only supports a limited subset of HTML and CSS markup. Again, HTML isn't native Flash code. I trust you're capable of googling to find out which tags are supported.

    Things that you're using that are not supported: <head> & <h1>
    So, you'll need to remove those from the markup.

    Also....
    You're getting way too fancy with the ActionScript. It's much easier to use an external CSS file.

    I'd never use HTML with Flash, but if I had to do what you're doing by the same techniques, this is how I would do it....

    Three-way interaction between Flash & ActionScript, HTML, & CSS.

    faq.html
    HTML Code:
    <html>
    <body>
    
    <p class="question" >This is the first question?</p>
    <p class="answer" >This is the first answer.</p>
    <p class="question" >This is the second question?</p>
    <p class="answer" >This is the second answer.</p>
    <p class="question" >This is the third question?</p>
    <p class="answer" >This is the third answer.</p>
    
    </body>
    </html>
    faq.css
    Code:
    .question {
      font-family: Arial,Helvetica,sans-serif;
      font-size: 12px;
      font-weight:bold;
      color:#ee0000;
    }
    
    .answer {
      font-family: Arial,Helvetica,sans-serif;
      font-size: 12px;
    }
    actionscript
    Code:
    var styles = new TextField.StyleSheet();
    styles.load("faq.css");
    
    textarea.html = true;
    textarea.styleSheet = styles;
    
    var lv:LoadVars = new LoadVars();
    
    lv.onData = function(content) {
      textarea.text = content;
    }
    
    lv.load("faq.html");
    Another tip: Use a dynamic text field instead of the textarea component.

  5. The Following User Says Thank You to Medyman For This Useful Post:

    ReadyToLearn (05-03-2008)

  6. #5
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i see what you mean when you say the HTML support is anything but full featured. I want to thank you for the effort you already put into helping me create what i desire to create. Your code did work, but I don't think the look is acceptable, and its definitely not flexible.

    i guess i will now attempt to take another approach. what do you think about XML? if i could get the data into my movie clip with XML, I could turn each question/answer pair into a custom class, and store an array of these. Then i could create a movie clip with many TextFields inside. Then i could add this new movieclip as the target for a ScrollArea component. Seeing as you have some more experience, what do you think of such an approach?

  7. #6
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Your code did work, but I don't think the look is acceptable, and its definitely not flexible.
    What kind of flexibility are you trying to achieve? In your previous post, you mentioned you wanted to be able to control font weight and color. This method certainly lets you to that much.

    what do you think about XML? if i could get the data into my movie clip with XML, I could turn each question/answer pair into a custom class, and store an array of these. Then i could create a movie clip with many TextFields inside. Then i could add this new movieclip as the target for a ScrollArea component. Seeing as you have some more experience, what do you think of such an approach?
    Yes, that would work. It's much more complicated, of course. Are you trying to control the positioning of the question/answer pairs? Using XML in this instance doesn't have any inherent benefit over text files.

Bookmarks

Posting Permissions

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