ActionScript 3 MovieClip 类链接

发布于 2024-08-01 19:28:39 字数 1235 浏览 4 评论 0原文

我只是使用 Flash CS3 Pro 来玩转基本的 ActionScript 3。

我在关键帧中放入了这个非常简单的代码来复制 n 个“brander”符号:

for (var i:Number=0; i<20; i++) {
    var m = new brander("MS_"+i);
    addChild(m);
    m.name = "MS_"+i;
    m.x = 20*i;
    m.alpha = a;
    a-=0.05;
    m.y = 20;
}

该符号链接到brander.as 类。

这个类是这样的:

package {
    import flash.display.*;
    public class brander extends MovieClip {
        var n:String;
        //
        public function brander(name) {
            setName(name);
        }
        //
        function setName(name) {
            this.n = name;
        }
        //
        function getName() {
            return n;
        }
    }
}

而且也很简单。

现在:我注意到我无法在这个类中真正设置任何内容。 因此,当我调用 setName (在创建“brander”实例时)时,我没有设置任何内容。 这可能吗?

我在没有调试的情况下进行了测试,只需编写:

btn.addEventListener(MouseEvent.MOUSE_DOWN, test);
//
function test(EVT) {
    trace(this.getChildByName("MS_2").getName());
}

当这个类无法存储信息时,为什么我们要链接一个类? 我究竟做错了什么?


编辑:

我发现这是有效的:

function fun(EVT) {
    trace((this.getChildByName("M_2") as brander).getName());
}

但我不明白为什么:你能告诉我为什么吗?

I'm simply playing around with basic ActionScript 3 using Flash CS3 Pro.

I put in a keyframe this very simple code to duplicate n "brander" symbols:

for (var i:Number=0; i<20; i++) {
    var m = new brander("MS_"+i);
    addChild(m);
    m.name = "MS_"+i;
    m.x = 20*i;
    m.alpha = a;
    a-=0.05;
    m.y = 20;
}

The symbol is linked to brander.as class.

The class is this one:

package {
    import flash.display.*;
    public class brander extends MovieClip {
        var n:String;
        //
        public function brander(name) {
            setName(name);
        }
        //
        function setName(name) {
            this.n = name;
        }
        //
        function getName() {
            return n;
        }
    }
}

and it is simple too.

Now: I noticed I can't really set anything in this class. So, when I call setName (at the creation of a "brander" instance), I don't set anything. Is this possible?

I tested without debugging, by simply writing:

btn.addEventListener(MouseEvent.MOUSE_DOWN, test);
//
function test(EVT) {
    trace(this.getChildByName("MS_2").getName());
}

Why do we link a class when this class can't store information? What am I doing wrong?


EDIT:

I found this is working:

function fun(EVT) {
    trace((this.getChildByName("M_2") as brander).getName());
}

but I can't understand WHY: could you please tell me why?

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

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

发布评论

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

评论(1

猥琐帝 2024-08-08 19:28:39

原因是 getChildByName() 函数返回一个 DisplayObject。 DisplayObject 没有 getName 函数。 然而,brander 类继承(扩展)了 DisplayObject,因此您可以将其存储为 DisplayObject。 但是,如果您想调用任何 Brander 函数,则需要首先使用 as 将其强制转换为 Brander。

有很多关于铸造多态性继承互联网上的几个地方。

The reason is that the getChildByName() funcction returns a DisplayObject. The DisplayObject has no getName function. The brander class however inherits from (extends) the DisplayObject, and therefore you can store it as a DisplayObject. But if you want to call any of the brander functions, you need to cast it to brander first, using as.

There is lots of information on casting, polymorphism and inheritance several places on the internet.

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