从 MovieClip 中读取 属性 的最佳方式?

Best way to read a property from a MovieClip?

我有一个 .fla 文件,其中我在场景中放置了一些动画片段实例。我需要遍历它们并收集一些数据,例如位置、名称和自定义属性。

这些自定义属性,我不知道如何传递它们,我知道目前有效的一种方法是使用辅助功能属性面板 (Flash Pro CC),然后在代码中我可以读取它们.但是我认为应该有更好的方法。

首先,您可以通过代码设置时间轴实例的属性。这没什么特别的。例如:

  1. 将库元件的实例放在关键帧上
  2. 在“属性”面板中为其指定一个实例名称,例如"myInstance"
  3. 在同一个关键帧上放置一些引用它的代码,例如myInstance.color = "red"

您还可以通过将符号设为组件来创建和分配自定义属性:

  1. 右键单击库中的符号并选择 "Component Definition"
  2. 在参数中添加自定义属性 table。它现在是一个组件符号。
  3. 在时间轴上,放置一个元件实例并使用“属性”面板设置其参数。

如果需要,您可以对组件做更多的事情,例如实时预览和编译组件。可以在这里找到更多信息:http://www.adobe.com/devnet/flash/learning_guide/components/part03.html

如果我正确理解了你的问题以及你在对@Aaron 的回答的评论中所说的话,你有一个动态加载的 swf 文件,并且你想 get/set 它的一些MovieClips 属性,如果是的话,就拿这个例子来说吧:

MyMC.as :

public class MyMC extends MovieClip 
{       
    private var timer:Timer;
    private var rotation_speed:int = 1;

    public function MyMC() {
    }
    public function set_Rotation_Speed(_rotation_speed:int): void {
        this.rotation_speed = _rotation_speed;
    }
    public function get_Rotation_Speed(): int {
        return this.rotation_speed;
    }
    public function start_Rotation(): void {            
        this.timer = new Timer(500, 10);
        this.timer.addEventListener(TimerEvent.TIMER, on_Timer);
        this.timer.start();
    }
    private function on_Timer(e:TimerEvent): void {
        this.rotation += this.rotation_speed;
    }
}

然后,在我的 swf.swf 中,我有一个该 MovieClip 的实例。

我使用以下代码加载了 swf.swf

var loader:Loader = new Loader()
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_SWFLoad);
    loader.load(new URLRequest('swf.swf'));

对于 set/get 我的一些 MovieClip 属性,我做了:

function on_SWFLoad(e:Event): void 
{       
    var swf:DisplayObjectContainer = DisplayObjectContainer(loader.content);
    var num_children:int = swf.numChildren;

    for(var i:int = 0; i < num_children; i++)
    {           
        var child:MovieClip = MovieClip(swf.getChildAt(i));

        // get the name
        trace('name : ' + child.name);

        // set the position
        child.x = child.y = 100;

        // get the class name, in my case it's MyMC
        var class_name:String = getQualifiedClassName(child);

        // get all the details of the child
        trace(describeType(child));

        child.set_Rotation_Speed(45);
        child.start_Rotation();

        trace(child.get_Rotation_Speed());      // gives : 45           
    }

    addChild(loader);
}

您可以使用 describeType() 函数获取实例的所有属性。

希望能帮到你。