PDA

View Full Version : Load swf


veiky
11-07-2006, 05:46 AM
Hi,

I have placed this code on click of a button


restart_btn.addEventListener(MouseEvent.CLICK, raceReset);
function pageReset(event:MouseEvent):void
{
var ldr:Loader = new Loader();
var url:String = "mypage.swf";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
addChild(ldr);
}


This is working fine. but when i click this button more than 3-4 times the speed of the page(some animation) goes down. I think that is because, I am loading the page again and again but not unloading it.

Any solution?

Thanks...

gellm
07-14-2008, 08:25 AM
if you store a reference to the DisplayObject you are loading then you can remove it next time you want to replace the item...then it won't add lots and lots of them.
//eg:


function pageReset(event:MouseEvent):void
{
var ldr:Loader = new Loader();
var url:String = "mypage.swf";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
if(childRef != null){
this.removeChild(childRef); //Here is the bit that removes it.
}
childRef = addChild(ldr); //returns a DisplayObject
}
//during script initialisation...
var childRef:DisplayObject = null;
restart_btn.addEventListener(MouseEvent.CLICK, raceReset);