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

发布于 2024-10-08 13:07:03 字数 1395 浏览 4 评论 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 中的所有小条动画剪辑都恢复为常规的旧动画剪辑并且不会触发。有趣的是,如果您在某个时刻将它们类型转换为 Bar,并且它们的普通类设置为 Bar(在影片剪辑本身内,而不是基类),它会起作用并且构造函数都会触发,如下所示:

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

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

但是,如果您想要多个不同类型的 Bar 剪辑,并给每个 Bar 基类,这是行不通的。它们都以通用影片剪辑的形式生成。如果您将它们强制转换为 Bar1Bar2,它确实有效,但这意味着在实践中您必须尝试将每个子级强制转换为每个顶级对象每次将剪辑添加到舞台时键入。

您也不能拥有一堆相同类型的影片剪辑,因为 Flash 不允许这样做,因此每个剪辑都需要是一个新类,并在将父级放在舞台上时对该类进行类型转换。有点违背了 OOP 的目的。

这似乎是Flash本身的一个bug,但是有什么办法可以修复它吗?

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. What's interesting is if you typecast one them as Bar at some point, and their normal class is set to Bar (within the movieclip itself, rather than base class), it works and the constructors all fire, like this:

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

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

However, if you want multiple different types of Bar clips, and give each a base class of Bar, this won't work. They all spawn as generic movieclips. It DOES work if you go through and typecast them as either Bar1 or Bar2, but that means in practice you would have to try to typecast every child as every top-level object type every time you added the clip to the stage.

You also can't have a bunch of movieclips of the same type since Flash doesn't allow it, so each one needs to be a new class, and have that class typecasted when the parent is placed on stage. Kind of defeats the purpose of OOP.

This seems to be a bug in Flash itself, but is there any way to fix it?

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

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

发布评论

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

评论(1

年华零落成诗 2024-10-15 13:07:03

问题在于您对 Foo 类成员的声明。如果您希望 Flash 将元素实例化为 MovieClip 或 Sprite 以外的任何元素,则必须向类中正确添加成员声明,​​或者在使用 new 关键字时声明类型。

这是有效的:

package {
import flash.display.MovieClip;
import flash.utils.describeType;

public class Foo extends MovieClip {
    public var bar1:Bar1;
    public var bar2:Bar2;
    public var bar3:Bar1;
    public var bar4:Bar2;

    public function Foo() {
        trace("foo", this.numChildren);

    }
}
}

如果您的类实际上没有做任何特殊的事情,您也可以将 ActionScript 首选项设置为“自动声明阶段实例”并省略类声明。

ActionScript 对类型相当宽容,但编译器似乎会忽略面板中声明的类型,直到在代码中找到某种引用,这可能是为了防止在有人忘记删除库中某处的类名时意外导致二进制文件膨胀。

The problem lies in your declaration of Foo's class members. If you want Flash to instantiate the elements as anything else than a MovieClip or Sprite, you must properly add a member declaration to the class, or declare the type when you use the new keyword.

This works:

package {
import flash.display.MovieClip;
import flash.utils.describeType;

public class Foo extends MovieClip {
    public var bar1:Bar1;
    public var bar2:Bar2;
    public var bar3:Bar1;
    public var bar4:Bar2;

    public function Foo() {
        trace("foo", this.numChildren);

    }
}
}

You can also just set the ActionScript preferences to "Automatically declare stage instances" and omit the class declaration, if your class doesn't really do anything special.

ActionScript is quite tolerant with types, but it seems the compiler ignores the types declared in the panel until it finds some kind of reference in the code, probably to prevent accidental bloating of the binary if someone forgot to erase a class name somewhere in the library.

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