我正在创建一个 MovieClip 子类(我们称之为 MyClip
),我想将其用于多个库资源。 我将通过 ActionScript 代码实例化这些影片剪辑。 MyClip
有一个构造函数参数,允许它设置某些属性的初始值。
由于我想将它用于多个库资源,因此逻辑方法似乎是在“符号属性”对话框的“基类”文本框中指定它。 问题是自动生成的子类没有带参数的构造函数。 相反,Flash 尝试仅使用默认构造函数生成它们,这也会失败,因为 MyClip
没有默认构造函数。
除了将属性初始化推迟到普通方法之外,还有什么办法可以解决这个问题吗?
编辑:我还不够清楚,我会尽力在这里澄清。 如果这是 MyClip
类:
public class MyClip extends MovieClip
{
private var someValue : Number;
public function MyClip(someValue : Number)
{
this.someValue = someValue;
}
}
并且我指定 MyClip
作为库中符号 MyClipA 的基类,我希望能够执行 clip = new MyClipA(17);
无需自己编写 MyClipA
类。
I'm creating a MovieClip subclass (let's call it MyClip
) that I want to use for several library assets. I'll be instantiating these movie clips from ActionScript code. MyClip
has a constructor parameter that allows it to set the initial values of certain properties.
Since I want to use it for multiple library assets, the logical way to do it seems to specify it in the "Base Class" text box in the "Symbol Properties" dialog. The problem is that the auto-generated subclasses don't have the constructor with the parameter. Instead, Flash tries to generate them with a default constructor only, which also fails because MyClip
doesn't have a default constructor.
Is there any way around this, apart from deferring property initialization to a normal method?
Edit: I haven't been clear enough, I'll try to clarify here. If this is the MyClip
class:
public class MyClip extends MovieClip
{
private var someValue : Number;
public function MyClip(someValue : Number)
{
this.someValue = someValue;
}
}
and I specified MyClip
as base class for symbol MyClipA in the library, I would ideally like to be able to do clip = new MyClipA(17);
without having to write the MyClipA
class myself.
发布评论
评论(4)
我假设您需要创建 MyClipA 和 MYClipB 等类的原因是将不同的艺术附加到仅具有 MyClip 对象行为的对象?
也许您可以以不同的方式处理它,将您的基类 - MyClip 作为您的示例,接受 2 个参数,其中之一是对艺术的引用:
并且您可以在应用于每个 ArtBaseClass 中定义您需要的任何其他内容这些艺术图书馆的物品。
I'm assuming the reason you need to create MyClipA and MYClipB, etc classes is to attach different art to objects that just have the behaviors of a MyClip object?
Maybe you could approach it differently, make your base class - MyClip as you have it your example, accept 2 aruguments, one of which is a reference to the art:
And you could define whatever else you need in a ArtBaseClass that is applied to each of these art library objects.
我不认为这真的可以做到。
但如果能做到的话,那就适当地用原型来实现吧。 也许您可以更改每个默认类的构造函数。
I don't think it can really be done.
But if it can be done, it will properly be with the prototype. Perhaps you can change the contructor of each of the default classes.
尝试为构造函数指定默认参数(
null
作为特殊值,也许?)。 然后在构造函数的开头添加一些错误检查,为 MovieClip 设置合理的默认值。 这确实很hacky,但据我所知,没有更好的方法来使用拖放来做到这一点。 删除动画 UI(只能通过代码来完成)。Try specifying default arguments for the constructor (
null
as a special value, perhaps?). Then add a bit of error checking at the beginning of the constructor that sets sensible defaults for your MovieClip. It's really hacky, but as far as I know there's not a better way to do it using the drag & drop animating UI (you can only do it through code).如果也遇到这种情况。 解决这个问题的正确方法是为MyClipA和MyClipB创建继承自MyClip的类。 如果有很多基于 MyClip 的类,这可能会很麻烦。 话又说回来,如果是这种情况,您可能想开始考虑使用组合而不是继承。
If ran into this as well. The proper way to solve it is to create classes for MyClipA and MyClipB that inherit from MyClip. This can be cumbersome if there are lot of classes that are based on MyClip. Then again, if that is the case you might want to start think about using composition instead of inheritance anyway.