如果影片剪辑是动态添加的影片剪辑的子级,则影片剪辑不会保留其类
我有一个电影剪辑支架,它的基类是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getChildAt() 返回一个 DisplayObject。为了访问任何 Bar 方法/属性,您需要将其显式转换为 Bar 对象。
使用您的代码作为示例...
奇怪的是,(非常奇怪)我刚刚注意到 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...
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.