Skip to main content
Participating Frequently
June 22, 2009
Answered

Problems with preloading external swfs

  • June 22, 2009
  • 1 reply
  • 1427 views

Hello

I have to make a website using Flash CS3 and ActionScript 3. I decided to set up a main.swf with a menu which loads all other pages (external swfs) into a movieclip. During the loading process a preloader is shown. This works fine until you click on a button for a page during another one is still loading. Allthough the percent counter refreshs and the swf you requested last gets loaded and dsiplayed, the preloader doesn´t turn invisible and stops above 100 %.
Please can anyone help me? Do I have to chose a totally different approach?

The code on frame 1:

var swfLoader:Loader = new Loader();
// sets the movieclip conbox as place for the external swfs
conbox.addChild(swfLoader);

// all external swfs
var homeURL:URLRequest = new URLRequest("Unterseiten/home.swf");
var home2URL:URLRequest = new URLRequest("Unterseiten/home2.swf");

// the EventListener
swfLoader.contentLoaderInfo.addEventListener(Event.OPEN, loadStart);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
// loads the first external swf on start up
swfLoader.load(homeURL);

// the functions
function loadStart(e:Event):void {
var percentLoaded = 0;
}

function loadProgress(event:ProgressEvent):void
{
    var percentLoaded:Number = event.bytesLoaded / event.bytesTotal;
    percentLoaded = Math.round(percentLoaded * 100);
    this.percentLoaded.text = String(uint(percentLoaded)) + "%";
    this.USLadetext.visible = true;
    this.percentLoaded.visible = true;
}
function loadProdComplete(e:Event):void {
trace("file loaded");
this.USLadetext.visible = false;
this.percentLoaded.visible = false;
}

The code for a button:

[button1.addEventListener(MouseEvent.MOUSE_UP, onBtn1Release);
function onBtn1Release(e:MouseEvent):void{   
swfLoader.load(home2URL);
}
This topic has been closed for replies.
Correct answer abrahamyan_com

I found an almost working solution without setting the loader to null. I make a var whis is set to "1" with the COMPLETE.event. If finished is "1" I have no problems unload the old page from the content box and calling the new one with swfLoader.load(URL2); - but I ´don´t know how to stop the loading process and load the new page.

The code for the pressed buttons:

button1.addEventListener(MouseEvent.MOUSE_UP, onBtn1Release); 
function onBtn1Release(e:MouseEvent):void{

if (this.finished == 1){
trace("No loading process at the moment. Loading new page.");
swfLoader.unload();
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(URL2);
}
else {
trace("There is a current loading process.");
swfLoader.close();
swfLoader.load(URL2);
}
}

ah now i see u are adding loader to display list.  I made an example of loading 2 swf files, here is code, source files are attached, let me know if u dont understand something.

stop(); //pages to load var swf_ar:Array =["content1.swf","content2.swf"] //holder sprite var holder:Sprite = new Sprite(); this.addChildAt(holder,0); //loader var loader:Loader; //controls btn1.addEventListener(MouseEvent.CLICK,onControlClick,false,0,true); btn2.addEventListener(MouseEvent.CLICK,onControlClick,false,0,true); function onControlClick(e:MouseEvent):void {      loadSection(Number(e.target.name.charAt(3))); } function loadSection(pSectionNum:uint):void {      //check of loader exist-> remove listeners ,close the loader,set to null      if(loader)      {           loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onSectionLoadComplete);           loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,onSectionLoadProgress);           loader.close();           loader=null;      }            loader = new Loader();      loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onSectionLoadComplete,false,0,true);      loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onSectionLoadProgress,false,0,true);      loader.load(new URLRequest(swf_ar[pSectionNum-1])); } function onSectionLoadComplete(e:Event):void {      if(holder.numChildren > 0)      {           holder.removeChildAt(0);      }            holder.addChild(MovieClip(e.target.content));            //when load finished remove losteners and set the loader to null      loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onSectionLoadComplete);      loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,onSectionLoadProgress);      loader=null; } function onSectionLoadProgress(e:ProgressEvent):void {      trace(e.bytesLoaded/e.bytesTotal); }

1 reply

Participating Frequently
June 22, 2009

first of all i think u have to remove listheners once ur page is loaded and set ur loader to null, for the next one u have to create them again.

Better if u put the instructions inside function.

I will not suggest to use Event.OPEN, i read once that its buggy, u dont need that event.

if you wanna during the middle of loading process load another page first of all u have to stop current process, u can do that using Loader.close() method.

Participating Frequently
June 22, 2009

Thanks for your answer! I updated my code. Now I remove the listener with the COMPLETE function. I removed the Event.OPEN. But... where exactly do I have to put in the swfloader.close() ? If I put it in the button function right before the request of the new page (swfloader.load(homeURL)) the behaviour of the preloader didn´t change. If swfloader.close() stands alone as the action of the button, no preloading process gets canceled.

Participating Frequently
June 22, 2009

when u request new page u have to check if u are in loading process? then close() loader, remoive listheners , set loader to null and start next one.

once ur loader complete u will remove all listheners and set the loader to null.

checking is simple: if(loader)   if its true it mean u are in middle of loading.