hRdCoder
hRdCoder

Reputation: 577

How do I unload or remove externally loaded SWF using Actionscript 3?

I'm working on a game in flash/actionscript, and I am having a bit of trouble on something in particular. I do have a somewhat limited knowledge in flash so that probably isn't helping me out much here, but anyways. In my game I have a help button. If this button gets clicked, a screen pops up showing instructions on what to do for the player. The instructions are divided into multiple SWF files, and so each time this button is clicked on, one of these external SWF files is loaded into the game for use.

I can do this part just fine, but if the player goes further into the game and clicks on the help button, the help is displayed, as well as every previously shown help file as well.

Obviously I don't want this to happen and I've tried so many things with no success.

Any suggestions are very much appreciated.

EDIT: Here is the code I'm using to load the help files.

private function prepHelp(title:String):void
    {   
        // introduction help files
        switch(title) {
            case "Welcome to Game Module 1": gameUI.singleton.loadHelp("Help/game_module1.swf"); break;
            case "Welcome to Game Module 2":    gameUI.singleton.loadHelp("Help/game_module2.swf"); break;
            case "Welcome to Game Module 3":    gameUI.singleton.loadHelp("Help/game_module3.swf"); break;
        }
    }

These call my loadHelp() function in the gameUI class:

public function loadHelp(file_name:String):void
    {
        helpBtn.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void { 

                _helpObject.loadHelp(file_name);
                singleton.addChild(_helpObject);
                _helpObject.dispatchEvent(new Event("getHelp"));
            } 
        );
    }

And so the help SWF is being loaded with my _helpObject object in my helpView.as class:

public class helpView extends Sprite
{   
    // variables associated with loading help content
    private var _helpLoader:Loader;
    private var _loader:Loader;
    private var _helpRequest:URLRequest;
    private var _helpMc:MovieClip = new MovieClip();

    private var _exitButton:Sprite = new Sprite();

    public function helpView():void {}

    public function loadHelp(help_file:String):void
    {
        _helpRequest = new URLRequest("swf/help_files/"+help_file);
        initialize();

        addEventListener("getHelp", getHelp);
    }

    private function initialize():void
    {   
        _exitButton.graphics.lineStyle(1, 0x000000, 1);
        _exitButton.graphics.beginFill(0xffffff);
        _exitButton.graphics.drawRect(0, 0, 75, 25);
        _exitButton.graphics.endFill();
        _exitButton.buttonMode = true;
        _exitButton.alpha = 1;

        _exitButton.x = gameUI.singleton.stage.stageWidth - 210;
        _exitButton.y = gameUI.singleton.stage.stageHeight - 55;

        _exitButton.addEventListener(MouseEvent.CLICK, closeHelp);
    }

    private function getHelp(evt:Event):void
    {   
        _helpLoader = new Loader();
        _helpLoader.load(_helpRequest);

        _helpLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showHelp);
    }

    private function showHelp(evt:Event):void
    {   
        _loader = Loader(evt.target.loader);

        // set the width and height of the help swf to the dimensions of the game window.
        _loader.content.width = gameUI.singleton.stage.stageWidth;
        _loader.content.height = gameUI.singleton.stage.stageHeight;

        _helpMc.addChild(_loader.content);
        _helpMc.addChild(_exitButton);
        addChild(_helpMc);
        setChildIndex(_helpMc, numChildren - 1);

        _helpLoader.removeEventListener(Event.COMPLETE, showHelp);
    }

    private function closeHelp(evt:MouseEvent):void
    {
        removeChild(_helpMc);
    }
}

Upvotes: 1

Views: 4230

Answers (3)

hRdCoder
hRdCoder

Reputation: 577

Okay I got it solved. What I did was I created objects that reference each SWF file, and threw them all into an array, along with a reference variable that keeps track of what SWF should be loaded. This reference variable tracks the current area of the game the player is in and so when the player clicks on the help button, the right SWF is loaded and shown to them and then can go right back to the game when they click Exit.

Thank you all for helping me on this.

Upvotes: 0

Mike
Mike

Reputation: 73

I think the issue you are having are orphaned swfs. So when a swf is loaded, it gets displayed, but is never unloaded when you are ready to move to the next swf (at least not unloaded properly)...

If you want to maintain this technique, you will need to make sure you unload the swf (properly, no leaks) when you are done using it.

In my games, generally the instruction screens are not so heavy that I would want to load and unload swf's, so I am not sure in your case, but one suggestion that I would make is that:

load all your swfs and information prior to the game being displayed (preloader stage) then have the game display or hide the appropriate help clip as desired.

IE:

  • have a preloader that loads your game along with the instruction swfs.
  • have the preloader provide a reference to the loaded instruction swfs to the game
  • have the game store the references in some array/vector.
  • if the user clicks the help button, display the first instruction swf, and set some "currentClip" variable to point to the first instruction swf
  • if the user clicks the close button, hide the "currentClip"
  • if the user clicks next instruction/prev instruction, hide the currentClip, show the next/prev instruction swf, and reinitialize the currentClip variable to the swf displayed...

If you can provide some source, I could probably help you more effectively.

Upvotes: 1

joshua
joshua

Reputation: 684

you could remove/unload the loader that loads theses help external swf..

addChild(yourloader)     //adds the loader 
removeChild(yourloader)   // removes your loader

Also if you used an array to store the Movie(SWFS) you could clear the array.lenght = 0; Probably a looping function will be more appropriate.

    var len:int = TempSWFS.length;
    for( var i:int = 0; i < len; i++ ) {
   if(TempSWFS[i] != null && TempSWFS[i].parent != null){
       TempSWFS[i].parent.removeChild(TempSWFS[i]);
   }

Upvotes: 0

Related Questions