ActionScript - 动态添加属性到 Sprite

发布于 2024-09-25 08:41:01 字数 1974 浏览 3 评论 0原文

我对以下问题感到非常困惑,如果能得到一些澄清,我将不胜感激。

一般来说,我的工作方式包括在 Flash Authoring 中设计所有图形,通过将基类更改为 flash.display.Sprite 将它们转换为 Sprite 符号,给我的实例命名,最后将它们导出到 ActionScript。

该方法实际上允许我在已导出到 ActionScript 的 Sprite 实例的代码中动态创建属性,就像它们是 MovieClip 的实例一样。我不完全确定为什么我能够做到这一点,但我可以。在轮询对象以确保其超类时,它们确实是 Sprite,而不是 MovieClip。

但是,正如预期的那样,如果我在代码中从头开始编程一个新的精灵,并尝试动态地将属性添加到新编程的精灵中,则会导致编译时错误。

package
{
import flash.display.Sprite;
import flash.utils.getQualifiedSuperclassName;

public class Document extends Sprite
    {
    public function Document()
        {
        trace(getQualifiedSuperclassName(blueOvalInstance));
        //flash.display::Sprite (it's not a MovieClip)

        trace(blueOvalInstance.hasOwnProperty("currentFrame"));
        //false (ok, ok, it's definately not a MovieClip)

        blueOvalInstance.myNewProperty = true;
        //dynamically added boolean property on a Sprite instance

        trace(blueOvalInstance.hasOwnProperty("myNewProperty"));
        //true.  fancy that!  my Flash Authoring exported Sprite has a dynamically added property

        codeSprite();
        }

    private function codeSprite():void
        {
        var myCodedSprite:Sprite = new Sprite();

        myCodedSprite.graphics.beginFill(0xFF0000);
        myCodedSprite.graphics.drawRect(0, 0, 100, 100);
        myCodedSprite.graphics.endFill();

        addChild(myCodedSprite);

        myCodedSprite.anotherNewProperty = true;
        //dynamically added boolean property on a Sprite instance, just like before!

        //Compile Time Error!!!

        //1119: Access of possibly undefined property anotherNewProperty through a reference with static type flash.display:Sprite.
        }
    }
}

那么,为什么我可以动态地将属性添加到我的文档类中导出的精灵(如果它们不是 MovieClip),而如果我自己在代码中创建它们则不能?

下图显示了使用 Sprite(不是 MovieClip)基类从 Flash 创作导出到 ActionScript 的新 BlueOval 元件。请注意库面板中新的绿色(而不是蓝色)“影片剪辑”图标。

替代文本

i'm pretty confused over the following issue and would be grateful for some clarity.

generally, how i work involves designing all of my graphics in Flash Authoring, converting them to Sprite symbols by changing the base class to flash.display.Sprite, give my instances names and finally export them to ActionScript.

the approach actually permits me to dynamically create properties in code on my Sprite instances that i've exported to ActionScript, just as if they were instances of MovieClips. i'm not entirely sure why i'm able to do this, but i can. in polling the objects to make sure of their superclass, they are indeed Sprites and not MovieClips.

however, as expected, if i program a new sprite from scratch in code and try to dynamically add a property to the new programmed sprite a compile time error will result.

package
{
import flash.display.Sprite;
import flash.utils.getQualifiedSuperclassName;

public class Document extends Sprite
    {
    public function Document()
        {
        trace(getQualifiedSuperclassName(blueOvalInstance));
        //flash.display::Sprite (it's not a MovieClip)

        trace(blueOvalInstance.hasOwnProperty("currentFrame"));
        //false (ok, ok, it's definately not a MovieClip)

        blueOvalInstance.myNewProperty = true;
        //dynamically added boolean property on a Sprite instance

        trace(blueOvalInstance.hasOwnProperty("myNewProperty"));
        //true.  fancy that!  my Flash Authoring exported Sprite has a dynamically added property

        codeSprite();
        }

    private function codeSprite():void
        {
        var myCodedSprite:Sprite = new Sprite();

        myCodedSprite.graphics.beginFill(0xFF0000);
        myCodedSprite.graphics.drawRect(0, 0, 100, 100);
        myCodedSprite.graphics.endFill();

        addChild(myCodedSprite);

        myCodedSprite.anotherNewProperty = true;
        //dynamically added boolean property on a Sprite instance, just like before!

        //Compile Time Error!!!

        //1119: Access of possibly undefined property anotherNewProperty through a reference with static type flash.display:Sprite.
        }
    }
}

so why is it that can i dynamically add properties to exported sprites in my document class if they are not MovieClips, while i can not if i create them myself in code?

the following image shows a new BlueOval symbol being exported to ActionScript from Flash Authoring with the base class of Sprite (not MovieClip). notice the new green (instead of blue) colored "Movie Clip" icon in the library panel.

alt text

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

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

发布评论

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

评论(2

幸福还没到 2024-10-02 08:41:01

您的类需要定义为动态类,以便在运行时添加属性。

查看此页面:http://flexmusings .wordpress.com/2008/07/23/actionscript-3-dynamic-classes-part-2/

You class needs to be defined as a dynamic class in order to have properties added during runtime.

Check out this page: http://flexmusings.wordpress.com/2008/07/23/actionscript-3-dynamic-classes-part-2/

匿名。 2024-10-02 08:41:01

var myCodedSprite:Sprite = new Sprite();
myCodedSprite 是密封类的对象,因此它无法在运行时添加属性。但是,当您扩展此类时,您可以在子类中添加自定义属性。

var myCodedSprite:Sprite = new Sprite();
myCodedSprite is an object of sealed class so it can not add property at runtime.. however when you extend this class , you can add custom properties in child class.

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