AS 3.0 动态实例名称

发布于 2024-11-01 21:27:53 字数 883 浏览 6 评论 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 技术交流群。

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

发布评论

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

评论(3

从来不烧饼 2024-11-08 21:27:53

首先,您需要将 Collector 声明为 public:

public var Collector:Array = new Array(); 

您的 Collector 是一个数组,而不是显示对象,因此无法将其添加到显示树中。相反,您可以将每个 newMovieClip 推送到 Custom 类的显示上,并将它们放置在 for 循环中。那么您根本不需要收集器,因为您可以使用 getChildAt() 来定位影片剪辑:

trace(customClass_mc.getChildAt(5).x);

Firstly, you need to declare Collector as public:

public var Collector:Array = new Array(); 

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():

trace(customClass_mc.getChildAt(5).x);
我只土不豪 2024-11-08 21:27:53

我自己找到了另一个答案,我认为它更好!

您根本不需要容器。

当您使用以下

package {

    import flash.display.MovieClip;
    public class CustomClass extends MovieClip {

        public function CustomClass(amount:uint) {  

            //Add and position Tiles to stage.
            for (var i:uint = 1; i <= amount; i++){ 
                var newMovieClip:MovieClip = new MovieClip;
                newMovieClip.y = amount * 10;
                newMovieClip.name = "clip"+i;
                addChild(newMovieClip); 
            }
        }
    }
}

否时,我可以使用以下方式访问影片剪辑:

var customClass_mc:CustomClass = new CustomClass(10);
addChild(customClass_mc);

//try to trace the x position of the fifth instance. 
trace(customClass_mc.getChildByName("child5").y);

I found another answer myself which i think is even better!

You don't need the container at all.

when you use the following

package {

    import flash.display.MovieClip;
    public class CustomClass extends MovieClip {

        public function CustomClass(amount:uint) {  

            //Add and position Tiles to stage.
            for (var i:uint = 1; i <= amount; i++){ 
                var newMovieClip:MovieClip = new MovieClip;
                newMovieClip.y = amount * 10;
                newMovieClip.name = "clip"+i;
                addChild(newMovieClip); 
            }
        }
    }
}

No i can acces the movieclips by using:

var customClass_mc:CustomClass = new CustomClass(10);
addChild(customClass_mc);

//try to trace the x position of the fifth instance. 
trace(customClass_mc.getChildByName("child5").y);
停滞 2024-11-08 21:27:53

变量“Collector”仅在构造函数内部可用。收集器必须公开才能从时间线之外访问。最好的办法是创建一个公共 getter 方法来访问它。所以像这样:

import flash.display.MovieClip;
public class CustomClass extends MovieClip {

    private var Collector:Array = new Array(); 

    public function get Collector():Array
    {
        return Collector;
    }

    public function CustomClass(amount:uint) {  

        //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);
    }
}

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:

import flash.display.MovieClip;
public class CustomClass extends MovieClip {

    private var Collector:Array = new Array(); 

    public function get Collector():Array
    {
        return Collector;
    }

    public function CustomClass(amount:uint) {  

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