简体   繁体   English

AS3链接-加载另一个SWF文件

[英]AS3 Linking-Loading Another Swf File

i made catching game and i have the swf file. 我做了捕捉游戏,并且有swf文件。 In another flash project, i want to call that swf. 在另一个Flash项目中,我想调用该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. 我认为我有此错误,因为在流行游戏中,我的fla和动作脚本位于不同的文件中。 I mean i use external .as file. 我的意思是我使用外部.as文件。 Not in the fla file. 不在fla文件中。

TypeError: Error #1009: Cannot access a property or method of a null object reference. TypeError:错误#1009:无法访问空对象引用的属性或方法。 at CatchingGame() 在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. 您的加载代码很好,但是异常源自CatchingGame代码,因此无法确定原因是什么。

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. 但是,如果(我猜),当它没有预装的最可能的原因展开你的游戏作品精细null object reference在这种情况下,太早期访问到stage从游戏性。 Try to move game initiation logic to ADDED_TO_STAGE event handler like in example below: 尝试将游戏启动逻辑移至ADDED_TO_STAGE事件处理程序,如下例所示:

    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. 如果我猜错了,请从CatchingGame swf添加更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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