# Help Optimizing Code - Dynamic text & images

**URL:** <https://forum.kirupa.com/t/help-optimizing-code-dynamic-text-images/297000>\
**Category:** flash\
**Created:** [September 23, 2009, 11:54pm UTC](https://forum.kirupa.com/t/help-optimizing-code-dynamic-text-images/297000 "2009-09-23T23:54:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![grey\_ghost](https://avatars.discourse-cdn.com/v4/letter/g/6a8cbe/32.png) [@grey\_ghost](https://forum.kirupa.com/u/grey_ghost)\
**Post date:** [September 23, 2009, 11:54pm UTC](https://forum.kirupa.com/t/help-optimizing-code-dynamic-text-images/297000/1 "2009-09-23T23:54:39Z")

</div>

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 🙂 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. 😉

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]

```auto
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]

```auto

//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]

```auto
//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]

```auto
//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]

```auto
//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"))
}

```
