orthopedic pain management

Flashuser

  • Pin It

Tips & Tricks 3: Loading of external resources such as images or SWFs using ActionScript 3.0

Maybe you have tried many different ways loading an external resource such as images or SWFs inside an empty movie clip from the stage and no matter what you have tried when loading another image / SWF again you saw two or three versions of it playing and hogging resources. Not good.

You first need to understand that this loading problem of an external image or SWF is a bug in how the removing a child / garbage collector / display list mechanisms are working in AS3 / Flash player and not an issue with the SWF of your product(s).

That’s because when you remove a child in Flash AS3, the reference to that child is still there in parent although you believed it was entirely removed. Here is a simple function that you can use in the future when you load images or SWFs inside an empty movie clip:

[as3]function loadResource(resURL) {

var url = resURL;
var request1 = new URLRequest(url);
var loader1 = new Loader();

var mc1 = this.getChildByName(“mc”);
var nc = mc1.numChildren;

if (nc) { var l1 = mc1.getChildAt(0); mc1.removeChild(l1); l1 = null; }

loader1.load(request1); //start loading img/swf
mc1.addChild(loader1);

loader1.contentLoaderInfo.addEventListener(“complete”, this.finished_loading);

}

function finished_loading(e:Event) { trace(“Loading completed.” }[/as3]

, where mc is the instance name of an empty movie clip from the stage. You can simply call this function with the path string to your external SWFs and images avoiding references duplicates.

Author: Alin

Passionate about web and graphic design, photography and climbing. Follow me at Twitter , Facebook and Google+

  • Eskel

    Thanks a lot *thumbs up*

  • haikal

    i’ve got question….maybe im noob with this thing…..where shud
    i put the code to load the swf/or image? currently im using..

    var myLoader:Loader = new Loader();
    addChild(myLoader);

    var url:URLRequest = new URLRequest (“.swf”);
    myLoader.load(url);

  • Use the loadResource method described above.