views:

76

answers:

2

In regards to as3 project: Is there a way to inherit the properties of a given DisplayObject? I am looking for a single method that will grab something like the x, y, width, height, color, etc. Whatever is involved in the common classes between the two display objects.

...

Edit:

I don't think I was clear enough... Let me give an example of the type of functionality I am looking for.

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);

So, in this case I know that the method 'inheritTransform()' doesn't exist, but I am wondering if there is something similar. Or maybe I am missing the point of extending a class in some way? I don't see how the two would relate in such a case.

Thanks, jml

+1  A: 

Actionscript 3 language reference says:
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.

bhups
Yeah... nobody reads the freaking language reference.
bug-a-lot
And it's a fairly good reference too!
Tegeril
I updated the content of my question- hopefully this clarifies what I am trying to do.
jml
+1  A: 

First at all, a TextField is a DisplayObject so, it have the x, y, width and height.

But, correct me if I'm wrong, if you wan't to "copy" the properties from one DisplayObject (or any object) to another you can do it manually, I think that there is no "automatic" way to do this. You can make use of a Prototype (or Prefab) to do this, but you have to implement it.

You can't inherit the value of a property at runtime, you inherit the property itself, but not his value (if the property is an Instance property, like x. y, width and height).

And other thing you may do is this:

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)

And other thing that may help you is the transform property of the DisplayObject class, that transform contains info about the position, rotation and scale (in a matrix) and the color information. This may help you.

In your example you can do this:

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: Sorry for my bad English.

EDIT:

You can only use addChild (and removeChild ) with DisplayObjectContainer, please see http://livedocs.adobe.com/flex/3/html/help.html?content=05_Display_Programming_03.html

unkiwii
wonderful help here. very informative... i would be interested if someone else finds that there's another more 'automatic' way of doing this. also whether there is a way to get the .transform method to work properly.
jml
i think for right now, the whateverDisplayObject.addChild(otherDisplayObject) will have to do, and works fairly well in terms of xy.
jml
The thing is that every DisplayObject has its own coordinate system, so when you add one to another (only the DisplayObjectContainer can do this) the childs now are IN the coordinate system of their father, so the matrix of the transform are concatenated (remember that the matrix has `x`, `y`, `scaleX`, `scaleY` and `rotation`
unkiwii