Preloading this dynamic text

So I’ve got a dynamic text box sucking in CSS styled HTML via a .txt file.

The text is “popping in” after the rest of the page has loaded sometimes since the .txt loads in after the preload.

What I’d like to do is have the animation fire after the text and .css are loaded.

Here’s the original AS:


import TextField.StyleSheet;

//import mx.tween

// Load the css file, then the text file (containing some html)

var cssUrl     :String = "fonts.css";
var htmlTextUrl:String = "news_1.txt";

var css:StyleSheet;

// Deal with a IE/Firefox inconsistency.
function fixUrl(url:String):String
{
    var rootUrl:String = _root._url;
    var iLastSlash:Number = rootUrl.lastIndexOf('/');
    if (iLastSlash > 0){
        rootUrl = rootUrl.substr(0,iLastSlash);    
    }
    return rootUrl + "/" + url;
}

function loadCSS():Void
{
    css = new StyleSheet();
    css.onLoad = function(bSuccess:Boolean):Void
    {
        if (bSuccess){
            loadHtmlText();
        }
    }
    css.load(fixUrl(cssUrl));
}
function loadHtmlText():Void
{
    // Use xml to load a text file.    
    var xml:XML = new XML();
    xml.onData = function(txt:String):Void {
        displayText(txt);
    };
    xml.load(fixUrl(htmlTextUrl));
}

function displayText(htmlText:String):Void
{
    var t:TextField = field_txt;
    t.html = true;
    t.styleSheet = css;
    t.embedFonts = true;
    t.autoSize = true;
    
    t.htmlText = htmlText;
}

loadCSS();


The mechanisms to control it seem to be there, but I’m missing something. So I was thinking something along these lines:

import TextField.StyleSheet;
stop();

this.onEnterFrame = function () {
        if (this.cssTextLoadComplete == true) {
                trace("DONE LOADING -- PUT AS below this line");
        } else {
                trace("STILL LOADING CSS AND TEXT ...");
        }
}

//import mx.tween

// Load the css file, then the text file (containing some html)

var cssUrl     :String = "fonts.css";
var htmlTextUrl:String = "news_1.txt";

var css:StyleSheet;

// Deal with a IE/Firefox inconsistency.
function fixUrl(url:String):String
{
        var rootUrl:String = _root._url;
        var iLastSlash:Number = rootUrl.lastIndexOf('/');
        if (iLastSlash > 0){
                rootUrl = rootUrl.substr(0,iLastSlash); 
        }
        return rootUrl + "/" + url;
}

function loadCSS():Void
{
        trace("FUNCTION loadCSS() firing ...");
        css = new StyleSheet();
        css.onLoad = function(bSuccess:Boolean):Void
        {
                if (bSuccess){
                        loadHtmlText();
                        trace("FUNCTION loadCSS() -- CSS loaded, firing loadHtmlText() ...");
                }
        }
        css.load(fixUrl(cssUrl));
}
function loadHtmlText():Void
{
        trace("FUNCTION loadHtmlText() firing ...");    
        // Use xml to load a text file. 
        var xml:XML = new XML();
        xml.onData = function(txt:String):Void {
                displayText(txt);
                trace("FUNCTION loadHtmlText() -- TEXT loaded, firing displayText() ...");
        };
        xml.load(fixUrl(htmlTextUrl));
}

function displayText(htmlText:String):Void
{
        trace("FUNCTION displayText() firing ...");     
        var t:TextField = field_txt;
        t.html = true;
        t.styleSheet = css;
        t.embedFonts = true;
        t.autoSize = true;
        
        t.htmlText = htmlText;
        
        //trigger the final loading guy
        this.cssTextLoadComplete = true;
        trace("FUNCTION displayText() finished -- setting cssTextLoadComplete var to true: " + this.cssTextLoadComplete);     
}

loadCSS();

Here’s a link to the .zip of the source files:

http://www.efcmusic.com/downloads/preload_txt.zip