Help Optimizing Code - Dynamic text & images

Thanks in advance to anyone that can help me optimize this code.

Summary:
I’ve created a banner w/ Flash CS4 & AS3 that cycles through 4 images & 4 text messages, all the input loaded dynamically. Thanks to some online tutorials & assistance from forums like this, it works :slight_smile: However, I feel there might be a better way to write this, like by reusing variables & objects, and possibly add garbage collection?? Any help here will go further in helping me understand AS3. :wink:

The banner mostly uses Tweens and the simple timeline to operate, but uses AS3 to switch the text & the images. The text is stored in a .txt file called “messages.txt” & the images are “image1.jpg”, “image2.jpg”, “image3.jpg”, & “image4.jpg”)

The text file contains the following variable definitions:
[FONT=Courier New]message_1=Blah blah&message_2=Yadda yadda&message_3=Poop poop&message_4=Stuff blah stuff[/FONT]

There is a dynamic text box called [FONT=Fixedsys]message_text[/FONT] & a movie clip called [FONT=Fixedsys]imageHolder_mc[/FONT] used for the images.

As it plays along the timeline, the following AS3 scripts are “activated” in the respective frames: (this is what I think could be optimized)

[LIST]
[*]Frame 1:
[/LIST]

var loader:URLLoader = new URLLoader()

//tell the loader that we are dealing with variables for the data 
loader.dataFormat = URLLoaderDataFormat.VARIABLES;

//after it is loaded completely, call the "loadData" function  
loader.addEventListener(Event.COMPLETE, loadData); 

 //Error handling    
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)
//loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus)

//the file to extract text messages from
loader.load(new URLRequest("messages.txt"));

//loading fucntion that imports the message & image
function loadData (event:Event):void { 
    message_text.text = loader.data.message_1
    var imgLoader:Loader = new Loader()
    imageHolder_mc.addChild(imgLoader)
    imgLoader.load(new URLRequest("image1.jpg"))
}

//error callbacks
function onIOError(evt:IOErrorEvent){
    trace("IOError: "+evt.text)
}

function onSecurityError(evt:SecurityErrorEvent){
    trace("SecurityError: "+evt.text)
}

function onHTTPStatus(evt:HTTPStatusEvent){
     trace("HTTPStatus: "+evt.status)
}

[LIST]
[*]Frame 47:
[/LIST]


//after it is loaded completely, call the "updateData2" function  
loader.addEventListener(Event.COMPLETE, updateData2);  

 //Error handling    
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)

//the file to extract from
loader.load(new URLRequest("messages.txt"));

//update function that imports the message & image
function updateData2 (event:Event):void { 
    message_text.text = loader.data.message_2
    var imgLoader:Loader = new Loader()
    imageHolder_mc.addChild(imgLoader)
    imgLoader.load(new URLRequest("image2.jpg"))
}

[LIST]
[*]Frame 97:
[/LIST]

//after it is loaded completely, call the "updateData3" function  
loader.addEventListener(Event.COMPLETE, updateData3);  

 //Error handling    
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)
    
//the file to extract from
loader.load(new URLRequest("messages.txt"));

//update function that imports the message & image
function updateData3 (event:Event):void { 
    message_text.text = loader.data.message_3
    var imgLoader:Loader = new Loader()
    imageHolder_mc.addChild(imgLoader)
    imgLoader.load(new URLRequest("image3.jpg"))
}

[LIST]
[*]Frame 141:
[/LIST]

//after it is loaded completely, call the "updateData4" function  
loader.addEventListener(Event.COMPLETE, updateData4);  

 //Error handling    
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)
    
//the file to extract from
loader.load(new URLRequest("messages.txt"));

//update function that imports the message & image
function updateData4 (event:Event):void { 
    message_text.text = loader.data.message_4
    var imgLoader:Loader = new Loader()
    imageHolder_mc.addChild(imgLoader)
    imgLoader.load(new URLRequest("image4.jpg"))
}

[LIST]
[*]Frame 186:
[/LIST]

//after it is loaded completely, call the "updateData1" function  
loader.addEventListener(Event.COMPLETE, updateData1);  

 //Error handling    
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)
    
//the file to extract from
loader.load(new URLRequest("messages.txt"));

//update function that imports the message & image
function updateData1 (event:Event):void { 
    message_text.text = loader.data.message_1
    var imgLoader:Loader = new Loader()
    imageHolder_mc.addChild(imgLoader)
    imgLoader.load(new URLRequest("image1.jpg"))
}