簡體   English   中英

從DisplayObject繼承屬性

[英]Inheriting properties from a DisplayObject

關於as3項目:有沒有一種方法可以繼承給定DisplayObject的屬性? 我正在尋找一種可以捕獲x,y,寬度,高度,顏色等內容的方法。無論兩個顯示對象之間的公共類涉及什么。

...

編輯:

我認為我不夠清楚...讓我舉一個我要尋找的功能類型的例子。

var sp1:Sprite = new Sprite();
sp1.x = 30;
sp1.y = 30;
sp1.width = 500;
sp1.height = 30;
var tf1:TextField = new TextField();
tf1.inheritTransform(sp1);

因此,在這種情況下,我知道方法'inheritTransform()'不存在,但是我想知道是否存在類似的東西。 還是我錯過了以某種方式擴展課程的意義? 在這種情況下,我看不出兩者之間的關系。

謝謝,jml

Actionscript 3語言參考說:
The DisplayObject class itself does not include any APIs for rendering content onscreen. For that reason, if you want create a custom subclass of the DisplayObject class, you will want to extend one of its subclasses that do have APIs for rendering content onscreen, such as the Shape, Sprite, Bitmap, SimpleButton, TextField, or MovieClip class.

首先, TextField是DisplayObject,因此它具有xywidthheight

但是,如果我錯了,請糾正我,如果您不想將屬性從一個DisplayObject(或任何對象)“復制”到另一個,則可以手動進行操作,我認為沒有“自動”方法可以做到這一點。 您可以使用Prototype(或Prefab)來執行此操作,但是您必須實現它。

你不能繼承在運行時屬性的值,你繼承財產本身,而不是他的價值(如果屬性為實例屬性,如xywidthheight )。

您可能要做的另一件事是:

var s:Sprite = new Sprite()
s.x = 125
s.y = 200
var t:TextField = new TextField()
s.addChild(t)  // here t transformation matrix is concatenated with s (because now t is inside s coordinate system because is a child of s)

另外可能對您有所幫助的是DisplayObject類的transform屬性,該transform包含有關位置,旋轉和比例(在矩陣中)以及顏色信息的信息。 這可能對您有幫助。

在您的示例中,您可以執行以下操作:

var s:Sprite = new Sprite()
s.x = 30
s.y = 30
s.width = 500
s.height = 30
var t:TextField = new TextField()
t.transform = s.transform // this overrides the transform object, but sadly don't work with the width and height
t.width = s.width
t.height = s.height

PS:對不起,我的英語不好。

編輯:

您只能將addChild (和removeChild )與DisplayObjectContainer ,請參閱http://livedocs.adobe.com/flex/3/html/help.html?content=05_Display_Programming_03.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM