AS 3.0 动态实例名称
您好,我创建了一个自定义类,我想在其中创建影片剪辑的 x 个实例。但以下不起作用:
包{
import flash.display.MovieClip;
public class CustomClass extends MovieClip {
public function CustomClass(amount:uint) {
var Collector:Array = new Array();
//Add and position Tiles to stage.
for (var i:uint = 1; i <= amount; i++){
var newMovieClip:MovieClip = new MovieClip;
newMovieClip.y = amount * 10;
Collector.push(newMovieClip);
}
addChild(Collector);
}
}
}
我想将它们放置在时间轴上,但
var customClass_mc:CustomClass = new CustomClass(10); addChild(customClass_mc); //try to trace the x position of one of the instances. trace(customClass_mc.Collector[5].x);
不断收到错误:场景 1,层“层 1”,帧 1,第 5 行 1119:通过静态类型 CustomClass 的引用访问可能未定义的属性收集器。
Hi i made a custom class where i would like to create x instances of a movieclip. But the following doesn't work:
package {
import flash.display.MovieClip;
public class CustomClass extends MovieClip {
public function CustomClass(amount:uint) {
var Collector:Array = new Array();
//Add and position Tiles to stage.
for (var i:uint = 1; i <= amount; i++){
var newMovieClip:MovieClip = new MovieClip;
newMovieClip.y = amount * 10;
Collector.push(newMovieClip);
}
addChild(Collector);
}
}
}
I would like to position them on the timeline with
var customClass_mc:CustomClass = new CustomClass(10); addChild(customClass_mc); //try to trace the x position of one of the instances. trace(customClass_mc.Collector[5].x);
I keep getting the error: Scene 1, Layer 'Layer 1', Frame 1, Line 5 1119: Access of possibly undefined property Collector through a reference with static type CustomClass.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您需要将 Collector 声明为 public:
您的 Collector 是一个数组,而不是显示对象,因此无法将其添加到显示树中。相反,您可以将每个 newMovieClip 推送到 Custom 类的显示上,并将它们放置在 for 循环中。那么您根本不需要收集器,因为您可以使用 getChildAt() 来定位影片剪辑:
Firstly, you need to declare Collector as public:
Your Collector is an array, not a display object, and so it can't be added to the display tree. Instead you would push each newMovieClip onto the display of Custom class and position them inside your for loop. Then you don't need the collector at all, because you can target the movieclips using
getChildAt()
:我自己找到了另一个答案,我认为它更好!
您根本不需要容器。
当您使用以下
否时,我可以使用以下方式访问影片剪辑:
I found another answer myself which i think is even better!
You don't need the container at all.
when you use the following
No i can acces the movieclips by using:
变量“Collector”仅在构造函数内部可用。收集器必须公开才能从时间线之外访问。最好的办法是创建一个公共 getter 方法来访问它。所以像这样:
The variable 'Collector' is only available inside the constructor the way you have it. Collector has to be made public to be accessible from outside the timeline. The best thing to do would be to make a public getter method to access this. So something like: