actionscript-3flashcs4

Need to unload an external swf from Array..cs4 as3


I have to turn some paintings into an animated flash SWF/game(s) and the someone else will turn it into an app.

I have an opening animation - this leads to a menu, where you can selct a few things - but right now the animation just plays through to the frame where the user has chosen Games.

I've made a game menu - when you select a game - the button loads the specific game into an empty MC (Called emptymc - instance name emptymc_MC)

The game loads up and covers the whole screen, apart from a menu bar on the top layer - which sits underneath at all times in the app. I haven't made these options yet - but it will most likely be a sound toggle - visit website - buy full app.

Ideally, I would want this to finish to the last frame and then unload or remove itself. Meanwhile, the SWF is revealed underneath. My problem is that I can't get rid of my imported SWF, Even when I load a new version into the loader.

stop(); 

bee.addEventListener(MouseEvent.CLICK, Click);
function Click( event:MouseEvent):void {
    gotoAndPlay(currentFrame+1);
}

bee.addEventListener(MouseEvent.CLICK,f);
function f(e:Event):void{
var movieArray:Array = ["howmanybees1", "howmanybees2", "howmanybees3",];
var loader:Loader = new Loader(); 
var index:int = movieArray.length * Math.random();
var url:String = movieArray[index] + '.swf'; 
trace("Attempting to load", url); 
loader.load(new URLRequest(url));    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete); 
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOError); 
addChild(loader); 

function loaderComplete(e:Event):void {     
    trace("Successfully loaded", url);
    } function loaderIOError(e:IOErrorEvent):void {     
    trace("Failed to load", url);    
}  
    }

** EDIT **

Here is my current source code:

stop();


  bee.addEventListener(MouseEvent.CLICK, Click);

  function Click(event: MouseEvent): void {
      gotoAndPlay(currentFrame + 1);
  }


  bee.addEventListener(MouseEvent.CLICK, f);

  function f(e: Event): void {
      var movieArray: Array = ["howmanybees1", "howmanybees2", "howmanybees3", "howmanybees4", "howmanybees5", ];
      var loader: Loader = new Loader();
      var index: int = movieArray.length * Math.random();
      var url: String = movieArray[index] + '.swf';
      trace("Attempting to load", url);
      loader.load(new URLRequest(url));
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
      loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOError);
      stage.addEventListener("UnloadGame", unloadGame);
      addChild(loader);


      function loaderComplete(e: Event): void {
          trace("Successfully loaded", url);
      }

      function loaderIOError(e: IOErrorEvent): void {
          trace("Failed to load", url);
      }

      function unloadGame(e: Event): void {
          trace("unloaded at last", url);
          loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
          loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderIOError);
          loader.removeEventListener("UnloadGame", unloadGame);
          removeChild(loader);
          loader.unloadAndStop();
      }
  }
  sheepgamebtn.addEventListener(MouseEvent.CLICK, Click2);

  function Click2(event: MouseEvent): void {
      gotoAndPlay(currentFrame + 1);
  }


  sheepgamebtn.addEventListener(MouseEvent.CLICK, i);

  function i(e: Event): void {
      var movieArray: Array = ["sheep1"];
      var loader: Loader = new Loader();
      var index: int = movieArray.length * Math.random();
      var url: String = movieArray[index] + '.swf';
      trace("Attempting to load", url);
      loader.load(new URLRequest(url));
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
      loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOError);
      stage.addEventListener("UnloadGame", unloadGame);
      addChild(loader);

      function loaderComplete(e: Event): void {
          trace("Successfully loaded", url);
      }

      function loaderIOError(e: IOErrorEvent): void {
          trace("Failed to load", url);
      }

      function unloadGame(e: Event): void {
          trace("unloaded at last", url)
          loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
          loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderIOError);
          loader.removeEventListener("UnloadGame", unloadGame);
          removeChild(loader);
          loader.unloadAndStop();
      }
  }

Solution

  • Loading a new swf doesn't affect a previously loaded swf at all (even if using the same variable). You need to tell Flash what to do.

    When finished with your loaded content, you should do three things:

    1. Remove it from the display:

      removeChild(loader);
      
    2. Remove any event listeners that aren't weakly referenced:

      loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderIOError); 
      
    3. Unload the loader:

      loader.unloadAndStop();
      

    Once those things are done, you can reuse the variable and load in new content.

    EDIT

    Here is an example of having your child swf trigger an unload:

    Inside the main timeline of your Loaded SWF Somewhere

    function unloadGame():void {
        if(stage) stage.dispatchEvent(new Event("UnloadGame"));
    }
    

    In your main swf

    loader.load(new URLRequest(url));            
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete); 
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOError); 
    stage.addEventListener("UnloadGame", unloadGame);
    addChild(loader); 
    
    function unloadGame(e:Event):void {
        loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loaderComplete); 
        loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderIOError); 
        loader.removeEventListener("UnloadGame", unloadGame);
        removeChild(loader); 
        loader.unloadAndStop();
    }