0
votes

I have a code that loads 4 swf files into main swf file one after another , but unfortunately I faced 2 problems : here is my code :

import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

progress_mc.scaleX = 0;

var loaderIndex:Number = -1;
var currentLoader:SWFLoader;

var swfs:LoaderMax = new 

LoaderMax({onComplete:completeHandler,onProgress:progressHandler,onChildComplete:childCompleteHandler});

swfs.append(new SWFLoader("part1.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("part2.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("part3.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("part4.swf", {container:container_mc, autoPlay:false}));




function progressHandler(e:LoaderEvent):void {
progress_mc.scaleX = e.target.progress;

}

function childCompleteHandler(e:LoaderEvent):void {
trace(e.target + " loaded");
e.target.content.visible = false;

}

function completeHandler(e:LoaderEvent):void {
trace("all swfs loaded");

progress_mc.visible = false;

initCurrentLoader();
addEventListener(Event.ENTER_FRAME, trackSWFPlayback);

}

function initCurrentLoader() {

loaderIndex++; 

if (loaderIndex == swfs.numChildren) {
    //reset back to 0 if the last swf has already played
    loaderIndex  =  0;
}

//dynamically reference current loader based on value of loaderIndex

currentLoader = swfs.getChildAt(loaderIndex);

//make the content of the current loader visible

currentLoader.content.visible = true;

//tell the current loader's swf to to play

currentLoader.rawContent.gotoAndPlay(1);

}

function trackSWFPlayback(e:Event):void {
//trace(currentLoader.rawContent.currentFrame);

//detect if the loaded swf is on the last frame

    if (currentLoader.rawContent.currentFrame == currentLoader.rawContent.totalFrames) {

    trace("swf done");

    //hide and stop current swf
    currentLoader.content.visible = false;

    currentLoader.rawContent.stop();


    //set up and play the next swf

    initCurrentLoader();


}

}

load_btn.addEventListener(MouseEvent.CLICK, loadSWFs)

function loadSWFs(e:MouseEvent):void{
load_btn.visible = false;
swfs.load();
}

problem 1 : part1.swf that is a swf file not made with adobe flash does't load. here is the error I can see each time :

RangeError: Error #2006: The supplied index is out of bounds. at flash.display::DisplayObjectContainer/getChildAt() at dblogo() at flash.display::Sprite/constructChildren() at flash.display::Sprite() at flash.display::MovieClip() at dbmovie() SWFLoader 'loader2' (part2.swf) loaded SWFLoader 'loader1' (part1.swf) loaded SWFLoader 'loader4' (part4.swf) loaded SWFLoader 'loader3' (part3.swf) loaded all swfs loaded swf done

poblem 2 : I have a line in my script that stops last swf . as I want to load swf files that maybe huge I rather that to unload instead of just stoping swfs . how to unload each swf instead of stoping it . as I'm completely new to flash and as3 can every one simply edit my as3 to fix these two main problems . Any Any Any Suggestions are EXTREMELY appreciated...Thanks a lot

1

1 Answers

0
votes

I believe this check is wrong - if you would only have one swf the numChildren would be 1 but its index would be 0.

Start with the index 0 instead of -1:

var loaderIndex:Number = 0;

And then:

if (loaderIndex == swfs.numChildren - 1) // index 0 equals numchildren = 1
{
    //reset back to 0 if the last swf has already played
    // Why do you reset the index if you only want to play the swfs once ? You should just stop here and remove your enterframe event listener
    loaderIndex  =  0;
}

loaderIndex++; 

As for unloading there is a way to remove the loader with swfs.remove(loaderInstance) but you would need to keep the instances of your loaders. In this case you would not need to increase your loaderIndex but always keep it at 0. Another way is swfs.dispose() which will get rid of ALL content in your LoaderMax (in this case you can only do this after the last swf have played)