如果影片剪辑是动态添加的影片剪辑的子级,则影片剪辑不会保留其类

发布于 2024-10-08 03:41:03 字数 819 浏览 3 评论 0原文

我有一个电影剪辑支架,它的基类是 foo。

package {
    import flash.display.MovieClip;

    public class Foo extends MovieClip {
        public function Foo() {
            trace("foo");
        }
    }
}

foo 中还有许多其他影片剪辑,其基类为 bar。

package {
    import flash.display.MovieClip;

    public class Bar extends MovieClip {
        public function Bar() {
            trace("bar");
        }
    }
}

我在 bar 的构造函数中添加了一条跟踪,这样我就可以知道它是否已正确加载,当我将 foo 拖到场景上并运行剪辑时,其中的所有小条都会正确触发。但是,当我动态地将其添加到场景中时,例如在 Main 类中:

package {
    import flash.display.MovieClip;

    public class Main extends MovieClip {
        public function Main() {
            this.addChild(new Foo());
        }
    }
}

突然, foo 中的所有小条动画剪辑都恢复为常规的旧动画剪辑并且不会触发。有什么办法可以解决这个问题吗?

I have a holder movieclip, its base class is foo.

package {
    import flash.display.MovieClip;

    public class Foo extends MovieClip {
        public function Foo() {
            trace("foo");
        }
    }
}

Within foo are a number of other movieclips, with a base class of bar.

package {
    import flash.display.MovieClip;

    public class Bar extends MovieClip {
        public function Bar() {
            trace("bar");
        }
    }
}

I put a trace in the constructor of bar so I can tell if it's being loaded properly, and when I drag out foo onto the scene and run the clip, all the little bars within it fire off correctly. However, when I add it to the scene dynamically, such as like this in the Main class:

package {
    import flash.display.MovieClip;

    public class Main extends MovieClip {
        public function Main() {
            this.addChild(new Foo());
        }
    }
}

Suddenly, all the little bar movieclips within foo revert to regular old movieclips and don't fire. Is there any way to fix this?

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

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

发布评论

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

评论(1

温柔戏命师 2024-10-15 03:41:03

getChildAt() 返回一个 DisplayObject。为了访问任何 Bar 方法/属性,您需要将其显式转换为 Bar 对象。

使用您的代码作为示例...

public class Main extends MovieClip {
    public function Main() {
        var foo:Foo = new Foo();
        stage.addChild(foo);
        foo.x = 0;
        foo.y = 0;

        trace(foo.getChildAt(1) as Bar);
    }//Main()
}

奇怪的是,(非常奇怪)我刚刚注意到 Flash 不会运行 Bar 的自定义构造函数,除非有代码将 Foo 中的这些对象声明为 Bar 类型。通过简单地修改上面的一条跟踪语句(以便它强制转换 getChildAt 的返回),Foo 中存在的所有 Bar 对象都运行其自定义构造函数。去算算吧。

getChildAt() returns a DisplayObject. In order to access any of the Bar methods/properties, you need to explicitly cast it as a Bar object.

Using your code for the example...

public class Main extends MovieClip {
    public function Main() {
        var foo:Foo = new Foo();
        stage.addChild(foo);
        foo.x = 0;
        foo.y = 0;

        trace(foo.getChildAt(1) as Bar);
    }//Main()
}

Oddly enough, (very oddly) I've just noticed that Flash doesn't run Bar's custom constructor unless there is code SOMEWHERE declaring those objects within Foo as being of the Bar type. By simply modifying the one trace statement above (so that it casts the return of getChildAt) all Bar objects present within Foo ran their custom constructors. Go figure.

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