Search code examples
actionscript-3flash

Actionscript - TLFTextField with External Text Files, Nothing Displays When File Runs


Total disclosure, this was an assignment for my class. However, this has already been turned in and graded. I really just want to know why it is not working.

I followed the steps in the book for the TLFTextField and external file. It works. I then needed to add 2 more TLFTextFields with 2 more external text files. When I now run it, the window pops up but is blank. Not even the original TLFTextField or text file display, which I know worked before. I did not even touch that code. There are no errors. I cannot figure out where I went wrong. My classmate had the same issue too.

My code is below:

import fl.text.TLFTextField;
import fl.controls.UIScrollBar;
import flash.net.URLLoader;
import flash.net.URLRequest;

import flash.events.Event;
import flash.text.TextFormat;
import flash.events.KeyboardEvent;

var t:TLFTextField = new TLFTextField();
var tf:TextFormat = new TextFormat();
t.width = 500;
t.height = 600;
t.background = true;
t.paddingTop = 20;
t.paddingLeft = 20;
t.paddingRight = 20;
addChild(t);

var textLoad:URLLoader = new URLLoader();
textLoad.addEventListener(Event.COMPLETE, textLoaded);
textLoad.load(new URLRequest("sample.txt"));

function textLoaded(e:Event):void
{
    var txt:String = URLLoader(e.target).data as String;
    t.text = txt;
    tf.color = 0x003300;
    tf.font = "Arial";
    tf.size = 14;
    t.setTextFormat(tf);
}

var cat:TLFTextField = new TLFTextField();
var cf:TextFormat = new TextFormat();
cat.width = 500;
cat.height = 600;
cat.background = true;
cat.paddingTop = 20;
cat.paddingLeft = 20;
cat.paddingRight = 20;
addChild(cat);

var catLoad:URLLoader = new URLLoader();
catLoad.addEventListener(Event.COMPLETE, catLoaded);
catLoad.load(new URLRequest("cat.txt"));

function catLoaded(e:Event):void
{
    var txt:String = URLLoader(e.target).data as String;
    cat.text = txt;
    cf.color = 0xFCE5CD;
    cf.font = "Arial";
    cf.size = 16;
    cat.setTextFormat(cf);
}

var doctor:TLFTextField = new TLFTextField();
var df:TextFormat = new TextFormat();
doctor.width = 500;
doctor.height = 600;
doctor.background = true;
doctor.paddingTop = 20;
doctor.paddingLeft = 20;
doctor.paddingRight = 20;
addChild(doctor);

var doctorLoad:URLLoader = new URLLoader();
doctorLoad.addEventListener(Event.COMPLETE, doctorLoaded);
doctorLoad.load(new URLRequest("doctor.txt"));

function doctorLoaded(e:Event):void
{
    var txt:String = URLLoader(e.target).data as String;
    doctor.text = txt;
    df.color = 0x68E9E5;
    df.font = "Arial";
    df.size = 16;
    doctor.setTextFormat(df);
}


var formatClip:Formatter = new Formatter();
var showFormat:Boolean = true;

stage.addEventListener(KeyboardEvent.KEY_DOWN, showFormatter);

function showFormatter(e:KeyboardEvent):void
{
    if (e.keyCode == 70)
    {
        if (showFormat)
        {
            addChild(formatClip);
            formatClip.x = t.width;
            formatClip.addEventListener(MouseEvent.MOUSE_DOWN, drag);
            showFormat = false;
        }
        else
        {
            formatClip.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
            removeChild(formatClip);
            showFormat = true;
        }
    }
}

function drag(e:Event):void
{
    formatClip.startDrag();
    formatClip.addEventListener(MouseEvent.MOUSE_UP, noDrag);
}

function noDrag(e:Event):void
{
    formatClip.stopDrag();
}

formatClip.fontList.addEventListener(Event.CHANGE, setFont);
formatClip.fontSizer.addEventListener(Event.CHANGE, setFontSize);
formatClip.colorPicker.addEventListener(Event.CHANGE, setColor);
formatClip.columnNum.addEventListener(Event.CHANGE, setColumns);

function setFont(e:Event):void
{
    tf.font = e.target.selectedItem.label;
    t.setTextFormat(tf);
    cf.font = e.target.selectedItem.label;
    cat.setTextFormat(cf);
    df.font = e.target.selectedItem.label;
    doctor.setTextFormat(df);
}

function setFontSize(e:Event):void
{
    tf.size = e.target.value;
    t.setTextFormat(tf);
    cf.size = e.target.value;
    cat.setTextFormat(cf);
    df.size = e.target.value;
    doctor.setTextFormat(df);
}

function setColor(e:Event):void
{
    tf.color = e.target.selectedColor;
    t.setTextFormat(tf);
    cf.color = e.target.selectedColor;
    cat.setTextFormat(tf);
    df.color = e.target.selectedColor;
    doctor.setTextFormat(tf);
}

function setColumns(e:Event):void
{
    t.columnCount = e.target.value;
    cat.columnCount = e.target.value;
    doctor.columnCount = e.target.value;
}

var scroller:UIScrollBar = new UIScrollBar();
scroller.move(t.x + t.width, t.y);
scroller.height = t.height;
scroller.scrollTarget = t;
addChild(scroller);
scroller.visible = false;

formatClip;
formatClip.addEventListener(MouseEvent.CLICK, setScrollbar);

function setScrollbar(e:Event):void
{
    if(t.textHeight > scroller.height)
    {
        scroller.visible = true;
    }
    else
    {
        scroller.visible = false;
    }
    t.scrollV = 1;
}

var catScroller:UIScrollBar = new UIScrollBar();
catScroller.move(cat.x + cat.width, cat.y);
catScroller.height = cat.height;
catScroller.scrollTarget = cat;
addChild(catScroller);
catScroller.visible = false;

formatClip;
formatClip.addEventListener(MouseEvent.CLICK, setCatScrollbar);

function setCatScrollbar(e:Event):void
{
    if(cat.textHeight > scroller.height)
    {
        catScroller.visible = true;
    }
    else
    {
        catScroller.visible = false;
    }
    cat.scrollV = 1;
}

var doctorScroller:UIScrollBar = new UIScrollBar();
doctorScroller.move(doctor.x + doctor.width, doctor.y);
doctorScroller.height = doctor.height;
doctorScroller.scrollTarget = doctor;
addChild(scroller);
doctorScroller.visible = false;

formatClip;
formatClip.addEventListener(MouseEvent.CLICK, setDoctorScrollbar);

function setDoctorScrollbar(e:Event):void
{
    if(doctor.textHeight > scroller.height)
    {
        doctorScroller.visible = true;
    }
    else
    {
        doctorScroller.visible = false;
    }
    doctor.scrollV = 1;
}

Solution

  • i am not at home atm. As far as i can see, the code looks solid, but i think i know, what happens there. Try to load the files in a row, that means, you load 1 file first, remove the listener, and after that, you load file 2, etc... I think that should work. It could be possible, that the event EVENT.Complete, trigger all the other listeners.