# Unload images

**URL:** <https://forum.kirupa.com/t/unload-images/327404>\
**Category:** flash\
**Created:** [February 27, 2012, 12:41pm UTC](https://forum.kirupa.com/t/unload-images/327404 "2012-02-27T12:41:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Modderov](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Modderov](https://forum.kirupa.com/u/Modderov)\
**Post date:** [February 27, 2012, 12:41pm UTC](https://forum.kirupa.com/t/unload-images/327404/1 "2012-02-27T12:41:20Z")

</div>

[COLOR=#333333]Hi everyone…[/COLOR]

[COLOR=#333333]i have this code:[/COLOR]

\*\*[COLOR=#FF0000]// i have nine buttons that activate doclick function[/COLOR]

for(var i:uint=1;i\<=numeroFoto;++i) {   
this[“bt”+i].addEventListener(MouseEvent.CLICK,doClick(i));  
}

\*\*[COLOR=#FF0000]// the function removes old image listeners, unloads it ,removes child and activates loadMv [/COLOR]

function doClick(imgNum:uint):Function {  
return listener;  
function listener(e:MouseEvent):void {   
if (caricatoreFoto != null) {  
caricatoreFoto.contentLoaderInfo.removeEventListen er(ProgressEvent.PROGRESS, caricatoreFotoProgress);  
caricatoreFoto.contentLoaderInfo.removeEventListen er(Event.OPEN, caricatoreFotoStart);  
caricatoreFoto.contentLoaderInfo.removeEventListen er(Event.COMPLETE, caricatoreFotoComplete);  
caricatoreFoto.contentLoaderInfo.removeEventListen er(Event.UNLOAD, caricatoreFotoUnloaded);  
}  
trace(“Unloading and removing process started…”);   
caricatoreFoto.contentLoaderInfo.addEventListener( Event.UNLOAD, caricatoreFotoUnloaded);  
caricatoreFoto.unloadAndStop()   
removeChild(caricatoreFoto)  
caricatoreFoto=null  
var mySwf = imgNum  
loadMv(“foto”+mySwf);  
}  
}

\*\*[COLOR=#FF0000]// this function loads the new image [/COLOR]

function loadMv(mySwfName ![](http://forum.kirupa.com/uploads/kirupa/6360/ca9da2d415402cfa.gif)tring):void {  
req = new URLRequest( mySwfName + “.png” );  
trace("Detected movie to load: " + mySwfName + “.png” );  
caricatoreFoto = new Loader();  
caricatoreFoto.contentLoaderInfo.addEventListener( Event.OPEN, caricatoreFotoStart);  
caricatoreFoto.contentLoaderInfo.addEventListener( Event.INIT, posizionacaricatoreFoto);  
caricatoreFoto.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, caricatoreFotoProgress);  
caricatoreFoto.contentLoaderInfo.addEventListener( Event.COMPLETE, caricatoreFotoComplete);  
caricatoreFoto.load(req);  
addChild(caricatoreFoto);  
trace("Movie " + mySwfName + “.png is being loaded”);  
}

\*\*[COLOR=#FF0000]// no mind about the following code… [/COLOR]

function posizionacaricatoreFoto (e:Event):void{  
caricatoreFoto.contentLoaderInfo.removeEventListen er(Event.INIT, posizionacaricatoreFoto)  
larghezzacaricatoreFoto=(caricatoreFoto.width)  
caricatoreFoto.x=(stageWidth-larghezzacaricatoreFoto)/2  
caricatoreFoto.y= posizioneYcaricatoreFoto   
trace (“caricatoreFoto.X=”+ (stageWidth/2-larghezzacaricatoreFoto/2))   
}

function caricatoreFotoStart(e:Event):void {  
prel\_mc.visible = true;  
preltxt\_txt.visible = true;  
}

function caricatoreFotoProgress(e ![](http://forum.kirupa.com/uploads/kirupa/5115/373eb333a028f173.gif)rogressEvent):void {   
var total = e.bytesTotal;  
var loaded = e.bytesLoaded;  
var percent = Math.round(loaded \* 100 / total);  
prel\_mc.scaleX=percent/100  
preltxt\_txt.text = String(percent) + “%”;  
trace(loaded + " / " + total + " = " + percent);  
}

function caricatoreFotoComplete(e:Event):void {

prel\_mc.visible = false;  
preltxt\_txt.visible = false;  
trace(“Loading complete”);  
}

function caricatoreFotoUnloaded(e:Event):void {  
loaded = 0;  
trace(“Content has been unloaded and ‘loaded’ variable is set to 0”);   
}

in the photoloader if you load a new photo (pushing the botton) when the previous one has completely loaded, there’s no problem…

but if you load a new photo while the previous loading is not complete, the system goes slowly, it seems that the LOADER.unloadAndStop and LOADER.removeChild is unuseful in that case, and the previous photo is still in the cache…  
I dont want just disable buttons during loading process (easy solution), but i’d like to solve the problem really unloading the image before a new one is loaded.

Sorry for bad english  
Thanks\*\*\*\*\*\*\*\*
