如何从加载的 swf 访问父 movieclip 的变量

发布于 2024-08-12 02:02:48 字数 2278 浏览 9 评论 0原文

我有一个加载外部 swf 的 swf。加载器 swf 有一个变量,我想从加载的 swf 访问该变量。我使用 MovieClip(parent.parent).myVar 但它返回此错误

TypeError:错误#1009:无法访问空对象引用的属性或方法。 在card01_fla::MainTimeline/frame1()

加载器 swf 有一个自定义文档类,我认为这是问题所在。

提前致谢。

编辑

这是主swf的as

    package 
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.text.TextField;

    /**
     * ...
     * @author chchrist
     */
    public class CardSender extends Sprite 
    {

        private var loader:Loader;
        private var viewButtonArray:Array;
        private var sendButtonArray:Array;
        private var cardContainer:Sprite;

        public var testVar:String

        public function CardSender() {

            testVar = "it worked!!!";
            init();
        }

        private function init() {
            viewButtonArray = [cardsenderMC.view01, cardsenderMC.view02, cardsenderMC.view03, cardsenderMC.view04];

            for each( var viewButton in viewButtonArray) {
                viewButton.buttonMode = true;
                viewButton.addEventListener(MouseEvent.CLICK, onViewClick);
            }
        }

        private function onViewClick(e:MouseEvent):void {
            var cardName = "card"+e.target.name.substring(4)+".swf";
            loader = new Loader();
            loader.load( new URLRequest(cardName));
            loader.contentLoaderInfo.addEventListener(Event.INIT, onCardInit);
        }

        private function onCardInit(e:Event):void {
            addChild(loader);
            var content:* = loader.content;
            content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
        }

        private function onCloseClick(e:MouseEvent):void {
            removeChild(loader);
        }

    }
}

,我尝试使用此代码从加载的swf访问测试变量

trace(MovieClip(parent.parent).testVar);

EDIT#2

如果我addChild(loader ) 不在 onCardInit 中,而是在 onViewClick 中,并且在加载的 swf 的第一帧中,我有 Object(parent.parent).testVar 它可以工作......但为什么它不起作用想要进入 onCardInit 函数内部吗?

I have a swf which load an external swf. The loader swf has a variable which i want to access from the loaded swf. I use MovieClip(parent.parent).myVar but it return this error

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at card01_fla::MainTimeline/frame1()

The loader swf has a custom Document Class which i think is the problem.

Thanks in advance.

EDIT

This is the as of the main swf

    package 
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.text.TextField;

    /**
     * ...
     * @author chchrist
     */
    public class CardSender extends Sprite 
    {

        private var loader:Loader;
        private var viewButtonArray:Array;
        private var sendButtonArray:Array;
        private var cardContainer:Sprite;

        public var testVar:String

        public function CardSender() {

            testVar = "it worked!!!";
            init();
        }

        private function init() {
            viewButtonArray = [cardsenderMC.view01, cardsenderMC.view02, cardsenderMC.view03, cardsenderMC.view04];

            for each( var viewButton in viewButtonArray) {
                viewButton.buttonMode = true;
                viewButton.addEventListener(MouseEvent.CLICK, onViewClick);
            }
        }

        private function onViewClick(e:MouseEvent):void {
            var cardName = "card"+e.target.name.substring(4)+".swf";
            loader = new Loader();
            loader.load( new URLRequest(cardName));
            loader.contentLoaderInfo.addEventListener(Event.INIT, onCardInit);
        }

        private function onCardInit(e:Event):void {
            addChild(loader);
            var content:* = loader.content;
            content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
        }

        private function onCloseClick(e:MouseEvent):void {
            removeChild(loader);
        }

    }
}

and I am trying to access the test variable from the loaded swf with this code

trace(MovieClip(parent.parent).testVar);

EDIT#2

If I addChild(loader) not in the onCardInit but in the onViewClick and in the first frame of the loaded swf i have Object(parent.parent).testVar it works...but why it doesn't want to be inside the onCardInit function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤者何惧 2024-08-19 02:02:48

您只有一个 loader 变量,并且要创建四个 Loader 实例并将它们分配给它 - 因此,如果您快速连续单击所有四个 Loader 实例,>loader 变量将有效包含与上次单击相对应的 Loader 对象。您的所有 addChild 调用都将作用于同一个 Loader 对象 - 其他对象将不会被添加,并且它们的父对象将继续为 null

进行以下操作代码更改:

private function onCardInit(e:Event):void 
{
    addChild(LoaderInfo(e.currentTarget).loader);
    var content:DisplayObject = loader.content;
    trace(content);
    trace(content.closeButton);
    content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
}

private function onCloseClick(e:MouseEvent):void 
{
    trace(e.currentTarget);//should be the closeButton
    trace(e.currentTarget.parent);//should be the loaded swf
    trace(e.currentTarget.parent.parent);//should be the Loader object that loaded it

    //The following will work only if last three traced correctly
    removeChild(e.currentTarget.parent.parent);
}

回答问题中的以下更新:

如果我在 onViewClick 中使用 addChild(loader) 而不是 onCardInit,则 Object(parent.parent).testVar< /code> 在加载的 swf 的第一帧中有效。为什么在 onCardInit 函数中 addChild 不起作用?


同样,这可能是因为您正在添加不同的 loader 对象 - 您是否单击了多个视图按钮?单击查看按钮并等待其加载以查看其工作情况。

You have just one loader variable and you're creating four Loader instances and assigning them to it - so if you are clicking all four of them in quick succession, the loader variable will effectively contain the Loader object corresponding to the last click. All your addChild calls will act on that same Loader object - the others will not be added and their parents will continue to be null

Make the following changes to the code:

private function onCardInit(e:Event):void 
{
    addChild(LoaderInfo(e.currentTarget).loader);
    var content:DisplayObject = loader.content;
    trace(content);
    trace(content.closeButton);
    content.closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
}

private function onCloseClick(e:MouseEvent):void 
{
    trace(e.currentTarget);//should be the closeButton
    trace(e.currentTarget.parent);//should be the loaded swf
    trace(e.currentTarget.parent.parent);//should be the Loader object that loaded it

    //The following will work only if last three traced correctly
    removeChild(e.currentTarget.parent.parent);
}

Answer to the following update in the question:

If I addChild(loader) in the onViewClick instead of onCardInit, then Object(parent.parent).testVar in the first frame of the loaded swf works. Why doesn't it work if I addChild inside the onCardInit function?

Again, that might be because you are adding a different loader object - are you clicking on more than one viewbuttons? Click on a view-button and wait till it is loaded to see it working.

弃爱 2024-08-19 02:02:48

父.父是错误的。
尝试使用:

MovieClip(MovieClip(e.currentTarget.parent).parent).myVar ;

您必须将第一个父级强制转换为某些类(例如 MovieClip),然后获取这个新 MovieClip 的父级,然后获取内部变量。

parent.parent is wrong.
try using:

MovieClip(MovieClip(e.currentTarget.parent).parent).myVar ;

You have to cast the first parent to some class like MovieClip and then get this new MovieClip's parent and then getting inside variable.

娇纵 2024-08-19 02:02:48

尝试追踪parent和parent.parent,我确信这是因为有一个额外的“parent”

try tracing parent and parent.parent, I'm sure it's because of an aditional "parent"

简单爱 2024-08-19 02:02:48

loader.content 不是影片剪辑,因此它不具有相同的功能。尝试

myLoadedClip:MovieClip = MovieClip(loader.content);

然后你的 myLoadedClip.parent.myVar 应该显示

你可能希望你的处理程序在 Event.COMPLETE 而不是在 Event.INIT 上触发

loader.content is NOT a movieclip, so it doesn't have the same capabilities. try

myLoadedClip:MovieClip = MovieClip(loader.content);

then your myLoadedClip.parent.myVar should show up

you might want your handler to fire on a Event.COMPLETE instead of on an Event.INIT

岁月打碎记忆 2024-08-19 02:02:48

加载的 swf 中的这行代码就解决了这个问题! ;)

MovieClip(MovieClip(parent.parent).root).play();

This only line code in loaded swf resolves it! ;)

MovieClip(MovieClip(parent.parent).root).play();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文