AS3库符号链接
我在使用创建的类作为库符号的基类时遇到问题:
我创建了一个类 AvSkin
,它将充当 AvChild< 实例的显示/代码>。它看起来像这样:
package avian.environment.skins
{
import flash.display.DisplayObject;
/**
* @author Marty Wallace
* @version 1.0.0
*/
public class AvSkin extends DisplayObject
{
/**
* Getters
*/
public function get top():Number{ return y - height/2; }
public function get left():Number{ return x - width/2; }
public function get bottom():Number{ return y + height/2; }
public function get right():Number{ return x + width/2; }
}
}
显然没有做很多事情,但重点是我可以稍后添加它(即 render()
方法)。
问题是,因为这扩展了 DisplayObject
(这样我就可以将皮肤设为 TextField
、SimpleButton
、Shape
,等)而不是 MovieClip
,如果我将其设置为库符号的基类,它会抛出此错误:
5000: 班级 'avian.environment.skins.AvSkin' 必须 子类“flash.display.MovieClip” 因为它链接到库符号 属于那种类型。
有办法解决这个问题吗?我不想执行以下任一操作:
- 使
AvSkin
扩展MovieClip
。 - 为我的库符号创建一个扩展
AvSkin
的类。
请参阅这里详细介绍了扩展 DisplayObject 的内容,这可能有助于支持我的问题背后的推理。
I'm having an issue using a class I've created as the base class for library symbols:
I've created a class AvSkin
which will act as the display for an instance of AvChild
. It looks like this:
package avian.environment.skins
{
import flash.display.DisplayObject;
/**
* @author Marty Wallace
* @version 1.0.0
*/
public class AvSkin extends DisplayObject
{
/**
* Getters
*/
public function get top():Number{ return y - height/2; }
public function get left():Number{ return x - width/2; }
public function get bottom():Number{ return y + height/2; }
public function get right():Number{ return x + width/2; }
}
}
Obviously doesn't do a lot, but the point is that I can add to it later on (ie a render()
method).
Problem is, because this extends DisplayObject
(so that I can make the skin a TextField
, SimpleButton
, Shape
, etc) and not MovieClip
, it throws this error if I set it as the base class for a library symbol:
5000: The class
'avian.environment.skins.AvSkin' must
subclass 'flash.display.MovieClip'
since it is linked to a library symbol
of that type.
Is there a way around this? I don't want to do either of the following:
- Make
AvSkin
extendMovieClip
. - Create a class for my library symbol that extends
AvSkin
.
See here for a detailed representation of what extends DisplayObject, which might help back the reasoning behind my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
库元件具有给定的类型,即
MovieClip
或Sprite
等。如果你想继承它们,你必须使用它作为基类。对此没有解决方法,它是不支持多重继承的语言的 OOP 范例,而 AS3 就是这样。您无法在继承树的中间扩展某些内容。对于您的任务,您可以有一个
接口
。但是您必须在所有派生类中实现此接口。但实现逻辑可以外包到静态类中以确保可维护性。Library symbols have a given type, i.e.
MovieClip
orSprite
and so on. If you want to inherit from these, you have to use this as base class. There is no workaround for this, it's an OOP paradigm for languages which do not support multiple inheritance, which AS3 is. You can't extend something in the middle of an inheritance tree.For your task you could have an
Interface
. But you have to implement this interface in all your derived classes. But the implementation logic could be out sourced in a static class to ensure maintainability.扩展Sprite怎么样?这是最简单的 DisplayObject 子类之一,不具备大多数 MovieClip 功能。
How about extending Sprite? That is one of the most barebones DisplayObject subclass that won't have most of the MovieClip functionalities.
只使用组合怎么样?
您可以实例化 AvSkin 并将您想要扩展的类发送到其中:
并像这样使用它:
现在您可以根据需要对其进行超类化。
How about just using composition?
You can instantiate the AvSkin and send the class you want to extend into it:
and use it like this:
Now you can superclass it as much as you'd like.