简体   繁体   中英

AS3 Linking-Loading Another Swf File

i made catching game and i have the swf file. In another flash project, i want to call that swf. I created a button and wrote the codes below.

btnn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_4);

import fl.display.ProLoader;
var fl_ProLoader_4:ProLoader;


var fl_ToLoad_4:Boolean = true;

function fl_ClickToLoadUnloadSWF_4(event:MouseEvent):void
{
    if(fl_ToLoad_4)
    {
        fl_ProLoader_4 = new ProLoader();
        fl_ProLoader_4.load(new URLRequest("CathingGame.swf"));
        addChild(fl_ProLoader_4);
    }
    else
    {
        fl_ProLoader_4.unload();
        removeChild(fl_ProLoader_4);
        fl_ProLoader_4 = null;
    }

    fl_ToLoad_4 = !fl_ToLoad_4;
}

But when i click the button, i have the error below. What could be the possible solutions? I think i have this error because in the catching game, my fla and actionscript are in different file. I mean i use external .as file. Not in the fla file.

TypeError: Error #1009: Cannot access a property or method of a null object reference. at CatchingGame()

Your loading code is fine, but exception is originated from the CatchingGame code, so that's not possible to say for sure what's the reason.

But, if (as I guess) your game works fine when it's launched without preloading the most probable reason for null object reference in that case is too earlier accessing to stage property from the game. Try to move game initiation logic to ADDED_TO_STAGE event handler like in example below:

    public function MyGame()
    {
        if (stage)
        {
            init();
        }
        else
        {
            addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
        }
    }

    private function onAddedToStage( event:Event ):void
    {
        removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
        init();
    }

    private function init():void
    {
        //place init logic here
    }

If my guess wrong, add more code from CatchingGame swf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM