1
votes

so I want to unload my 1st SWF before load 2nd SWF my problem when I load SWF is 2nd SWF is stack with 1st SWF and when I back from 2nd SWF to 1ST sound play will Stack too and 1st SWF's animation can still seen on 2nd SWF

1St.SWF Code:

var pausePoint:Number = 0.00;
var aPlaying:Boolean;
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound(new URLRequest("home.mp3"));

soundChannel = sound.play(0,999);
aPlaying = true;    
    var loader:Loader = new Loader();
    addChild(loader);
    
    btnInfo.addEventListener (MouseEvent.CLICK, loadInfo);
    function loadInfo(event : MouseEvent): void{
        removeChild(loader);
        loader.unloadAndStop();
        var SWFRequestInfo:URLRequest = new URLRequest("2nd.swf");
        loader.load(SWFRequestInfo);
        addChild(loader);
    }

2nd.SWF Code:

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

btnBack.addEventListener (MouseEvent.CLICK, loadBack);
function loadBack(event : MouseEvent): void{
    removeChild(loader);
    loader.unloadAndStop();
    var SWFRequestBack:URLRequest = new URLRequest("1st.swf");
    loader.load(SWFRequestBack);
    addChild(loader);
}

its there any solution to solve this problem?

Updated 2

//main.swf

    const FIRST:int = 1;
const SECOND:int = 2;
var current:int;
var loader1:Loader = new Loader();
var loader2:Loader = new Loader();
addChild(loader1);
addChild(loader2);
addEventListener(LoadEvent.LOAD_SWF, loadSWF);
trace("first Load");
loader2.visible=false;
loader1.visible=false;
loader1.load(new URLRequest("1.swf"));
loader1.visible=true;
current=FIRST;

function loadSWF(event:LoadEvent):void
{
    if(current == FIRST){
        current=SECOND;
        loader2.load(new URLRequest(event.url));
        loader2.visible=true;
        trace("loader2 Loaded");
        loader1.unloadAndStop(true);
        loader1.visible=false;
        trace("loader1 Unloaded");
    }
    else{
        current=FIRST;
        loader1.load(new URLRequest(event.url));
        loader1.visible=true;
        trace("loader1 Loaded");
        loader2.unloadAndStop(true);
        loader2.visible=false;
        trace("loader2 Unloaded");
    }
    
}

1.swf

function clickNext(event:MouseEvent):void{
    trace("next clicked");
    dispatchEvent(new LoadEvent(LoadEvent.LOAD_SWF, "2.swf"));
}

2.swf

function clickPrev(event:MouseEvent):void{
    trace("prev clicked");
    dispatchEvent(new LoadEvent(LoadEvent.LOAD_SWF, "1.swf"));
}

LoadEvent.as

package  {
    import flash.events.Event;  
    public class LoadEvent extends Event {
        
        public static const LOAD_SWF:String = "LoadSWF";
        
        public var url:String;
        
        public function LoadEvent(type:String, url:String,bubbles:Boolean=true, cancelable:Boolean=false) {
            super(type,bubbles,cancelable);
            this.url=url;
        }    
        
        public override function clone():Event
        {
            return new LoadEvent(type,url,bubbles,cancelable );
        }

    }
    
}
1

1 Answers

2
votes

Make 3-rd (main) SWF which will loads/unloads the 1-st and the 2-nd SWF. When you click at the btnBack or btnInfo you can dispatch some event. This event you can catch in the main SWF and unload current SWF and load next SWF.

UPDATE

This is a code of the main SWF:

const FIRST:int = 1;
const SECOND:int = 2;
var current:int;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHandler);
addChild(loader);
loadFirst();

function loadedHandler(event:Event):void
{
    loader.content.addEventListener(Event.CLOSE, closeHandler);
}

function closeHandler(event:Event):void
{
    if (current == FIRST)
    {
        loadSecond();
    }
    else if (current == SECOND)
    {
        loadFirst();
    }
}

function loadFirst():void
{
    if (loader.content)
    {
        loader.unloadAndStop(true);
    }
    current = FIRST;
    loader.load(new URLRequest("1st.swf"));
}

function loadSecond():void
{
    if (loader.content)
    {
        loader.unloadAndStop(true);
    }
    current = SECOND;
    loader.load(new URLRequest("2nd.swf"));
}

The next code is dispatched from the 1-st and the 2-nd SWF in the MouseEvent.CLICK handler:

dispatchEvent(new Event(Event.CLOSE));

UPDATE #2

const FIRST:int = 1;
const SECOND:int = 2;
var current:int;

var loader1:Loader = new Loader();
var loader2:Loader = new Loader();
loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded1Handler);
loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded2Handler);
addChild(loader1);
addChild(loader2);

loadFirst();

function loaded1Handler(event:Event):void
{
    loader1.content.addEventListener(Event.CLOSE, closeHandler);
    loader2.visible = false;
}

function loaded2Handler(event:Event):void
{
    loader2.content.addEventListener(Event.CLOSE, closeHandler);
    loader1.visible = false;
}

function closeHandler(event:Event):void
{
    if (current == FIRST)
    {
        loadSecond();
    }
    else if (current == SECOND)
    {
        loadFirst();
    }
}

function loadFirst():void
{
    if (loader1.content)
    {
        loader1.unloadAndStop(true);
    }
    current = FIRST;
    loader1.load(new URLRequest("1.swf"));
    loader1.visible = true;
}

function loadSecond():void
{
    if (loader2.content)
    {
        loader2.unloadAndStop(true);
    }
    current = SECOND;
    loader2.load(new URLRequest("2.swf"));
    loader2.visible = true;
}

UPDATE #3

const FIRST:int = 1;
const SECOND:int = 2;
var current:int;
var loader1:Loader = new Loader();
var loader2:Loader = new Loader();

loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loader1Compl);
loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, loader2Compl);

addChild(loader1);
addChild(loader2);
addEventListener(LoadEvent.LOAD_SWF, loadSWF);
trace("first Load");
loader2.visible=false;
loader1.visible=false;
loader1.load(new URLRequest("1.swf"));
loader1.visible=true;
current=FIRST;

function loader1Compl(event:Event):void
{
    loader2.unloadAndStop(true);
    loader2.visible=false;
    trace("loader2 Unloaded");
}

function loader2Compl(event:Event):void
{
    loader1.unloadAndStop(true);
    loader1.visible=false;
    trace("loader1 Unloaded");
}

function loadSWF(event:LoadEvent):void
{
    if(current == FIRST){
        current=SECOND;
        loader2.load(new URLRequest(event.url));
        loader2.visible=true;
        trace("loader2 Loaded");

    }
    else{
        current=FIRST;
        loader1.load(new URLRequest(event.url));
        loader1.visible=true;
        trace("loader1 Loaded");
    }

}