在Flash AS3设计组件 [英] AS3 Components in Flash Designer

查看:181
本文介绍了在Flash AS3设计组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ActionScript 2的项目,我可以创造一个新的MovieClip,右键点击它,在库,选择组件定义补充一点,可以在影片剪辑内引用参数。此参数可以在影片剪辑的属性很容易地改变。

In an ActionScript 2 project I can create a new MovieClip, right-click on it on the library and select "Component Definition" to add parameters that can be referenced inside the MovieClip. This parameters can be easily changed in the MovieClips's properties.

现在,我工作的一个ActionScript 3的项目,但一直没能想出一个方法来获得这些参数传递的值。

Now, I'm working on an ActionScript 3 project but haven't been able to figure out a way to obtain the values passed in those parameters.

我定义了一个名为textToDisplay的参数,但是当我在写操作的第一帧下面我得到一个错误:

I defined a parameter named "textToDisplay" but when I write the following in the Actions for the first frame I get an error:

trace(textToDisplay);

这是错误:

1120: Access of undefined property textToDisplay.

你知道如何捕捉该参数的值?

Do you know how to capture the value of that parameter?

感谢

PS:我使用的Adobe Flash CS3专业版的Windows XP

PS: I'm using Adobe Flash CS3 Professional on Windows XP

推荐答案

在AS3中,你必须有一个或多个公共变种声明创建外部类文件,你会使用(你也可以使用公共 GET / 设置函数)。谷歌这一点,如果你不知道怎么样。

In as3 you have to create an external class file with one or more public var declarations that you will use (you can also use public get/set functions). Google this if you're not sure how.

在外部文件中,使用 [视察] 的元数据标记只是你的 VAR之前,像这样的:

In your external file, use the [Inspectable] metadata tag just before your var, like this:

package myPackage {
  import flash.display.MovieClip;

  public class MyComponent extends MovieClip {

    [Inspectable]
    public var myFancyComponentParameter:String;

    [Inspectable]
    public var myOtherFancyComponentParameter:int;

  }

}

然后就可以打开组件定义对话框中,设置类字段外部类的名称(包括包名),和Flash会自动创建一个基于你的组件参数的 [视察] 标签。或者你也可以手动创建它们。

Then you can open the Component Definition dialog, set the Class field to the name of your external class (including package name), and Flash will automatically create parameters for your component based on your [Inspectable] tags. Or you can create them manually.

一旦你设置这件事,你可以访问该组件瓦尔在时间轴code:

Once you've set this up, you can access the component vars in timeline code:

trace("Here's my var: " + myFancyComponentParameter);

上的 [视察] 标签(包括数据类型)的详细信息可在的元数据的LiveDocs 的。

Details on the [Inspectable] tag (including data types) are available at the metadata livedocs.

这也是一个好主意,设置联动对话框中的类名也一样,如果你希望你的外部类做任何事情以外举行的元件值。

It's also a good idea to set the class name in the Linkage dialog too, if you want your external class to do anything other than hold component values.

我也建议你把code在外部类中,而不是时间轴。它更可扩展的方式。如果你这样做,只记得你的组件参数没有设置,直到 INIT 事件被触发后。以下是如何侦听:

I'd also recommend putting code in your external class, rather than in the timeline. It's more extensible that way. If you do this, just remember that your component parameters aren't set until after the INIT event is fired. Here's how to listen for that:

// package and import statements omitted for brevity
public class MyComponent extends MovieClip {

  [Inspectable]
  public var myFancyComponentParameter:String;

  public function MyComponent() {
    // myFancyComponentParameter not set here yet
    trace(myFancyComponentParameter); // prints null
    addEventListener(Event.INIT, onInit);
  }

  public function onInit(e:Event) {
    // now we can use component parameters!
    trace(myFancyComponentParameter); // prints the param value
  }

}

这篇关于在Flash AS3设计组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆