actionscript-3resetloaderflixel

AS3 Loader doesn't unload after game reset


you have always been of great help for me throughout the last few years. I could always find a solution to my programming problem. This time however I couldn't find a solution to this one. Although there are a few topics which discuss Loaders. Mine is a lot different.

The problem: In the first game screen I load a movie (a movie converted to .swf). And after I switch to the next state, it unloads and doesn't display any more. At the end of the game I execute my own made reset function (which has not much to do with the loader whatsoever, it just resets some static variables) and the game switches to the first state.

The Loader once again shows up. But now when I switch states, the Loader is still displaying.

Note: I am using the flixel library. Although I don't think it makes a big difference in this case (though I figure it problably does since I still have the problem :P).

I have only placed the important code. I have three buttons. Let's say I push. the third button. In the switch case I enter case 2.

The reset function is a flixel function: FlxG.resetGame() but I figure it doesn't really matter.

I hope this is enough information for someone to see the problem/solution. Thanks in advance.

Here's the code:

AMCInlog.as

private var my_loader:Loader;
private var my_player:Object;
    public function AMCInlog() 
    {
        imgBackground = new FlxSprite(0, 0, Resources.imgStartMenuBackground);
        add(imgBackground);

        my_loader = new Loader();
        my_loader.x = 40;
        my_loader.y = 156;          
        my_loader.scaleX = 1.2;
        my_loader.scaleY = 1.2;
        //loadUserData();
        moviePlayerSequence();
        //userData.clear();
        //createButtons();  
    }

    private function moviePlayerSequence():void {
        var request:URLRequest = new URLRequest("DGAME Movie.swf");
        my_loader.load(request);
        FlxG.stage.addChild(my_loader);
    }

    private function currentButtonFunction():void {
        switch(currentButton) {
            case 0:
                my_loader.unloadAndStop();
                FlxG.stage.removeChild(my_loader);
                FlxG.switchState(new AMCRegister());
                break;
            case 1:
                my_loader.unloadAndStop();
                FlxG.stage.removeChild(my_loader);
                FlxG.switchState(new AMCExistingAccountLogin());
                break;
            case 2:
                my_loader.unloadAndStop();
                FlxG.stage.removeChild(my_loader);
                FlxG.switchState(new AMCPreferences());
                break;
            default:
                break;
        }
    }   

Solution

  • You never reset your loader, hence it just holds onto what it loaded last time, either simply try and reset (or re-initiate actually) your loader like so (not the best method (will go into flash garbage-collection which has never been that awesome) but hey, it works)

    my_loader = new Loader();
    

    Or (since i dont see it) try using simply unload(); instead of unloadAndStop(); (since well, you dont need to stop it, if you unload it properly, its gone anyway)

    Good luck!