PDA

View Full Version : adding a child does not change its parent's coordinate values


smohadjer
10-20-2009, 06:35 PM
I expected the trace command at the end of this code to return 0, but it returns 100. Does anyone know why mc's y coordinate value is not updated after adding it a child?

var mc:MovieClip = new MovieClip();
var t:TextField = new TextField();

t.text = "text 1";
t.y = -100;
mc.y= 100;

mc.addChild(t);
trace(mc.y);

ASWC
10-20-2009, 06:41 PM
Adding children to a container does not change the container coordinates, it does change the container registration point eventually but that's it.

smohadjer
10-21-2009, 03:31 PM
But that seems wrong because the container is now starting at y=0 and not at y=100 if you look at the stage.

Why shouldn't adding children to a container change the container's x and y properties when it does change it's other properties such as width and height?

Thanks
Saeid

ASWC
10-21-2009, 04:27 PM
1. If it was changing its coordinates then we will never be able to change its registration point.
2. If you were to add an object while the movieclip is transitioning then you will see a big jump in the transition since suddenly the x,y have changed.

henke37
10-21-2009, 07:41 PM
It's very simple, it is a matter of coordinate systems being nested.

smohadjer
10-22-2009, 02:29 AM
Is it possible to get the registration point of the container (mc) with actionscript before and after child is added?

ASWC
10-22-2009, 03:45 AM
Sure you just need to use the object getRect() method and find out the difference between the object x, y coordinates and the rectangle x,y coordinates.

henke37
10-22-2009, 07:46 PM
Call localToGlobal with the argument (0,0) on the object that you want the registration point on. Then call globalToLocal on object of whatever code wondered.

GetRect does not return the registration point at all, it returns the bounding box of the contents (ignoring strokes). That is not related to the registration point at all.

ASWC
10-22-2009, 08:00 PM
GetRect does not return the registration point at all, it returns the bounding box of the contents (ignoring strokes). That is not related to the registration point at all.
That's quite an affirmation but in fact it is totally related. The rectangle that will be returned will have top left coordinates and from that you can easily get the real coordinates of the object by simple subtraction. I use this in many of my classes so I kinda know it works.

henke37
10-23-2009, 08:27 AM
It does indeed have the top left coordinate of the bounding box. So what? The registration point may be outside the bounding box or inside of it. It can be above or bellow the bounding box. All that you know for sure is that the registration point is at (0,0) in the coordinate system used by the container that it belongs to.

ASWC
10-23-2009, 02:59 PM
I advise you to look at this tutorial where unknown registration are found using the getRect() method:
http://www.actionscript.org/resources/articles/916/1/Creating-a-Reflection-Effect-with-AS3/Page1.html
You might learn a thing or two.

henke37
10-24-2009, 08:09 PM
That doesn't find the registration point, it only uses complicated math to try and deal with the real issue, that the contents of the object is not related to the registration point. It does things like offsetting, rotating and scaling, but it never calculates the registration point. Why? Because it is already known to be at (0,0).

senocular
10-24-2009, 08:38 PM
There seems to be a lot of confusion here. I'm going to try to clear things up:

The x and y coordinates of a display object, such as a text field, indicate that object's position within it's parent. If that object's parent is moved, it's x and y properties do not change even though visually that object has moved in the screen.

To get the global coordinates of an object, you would use localToGlobal. This takes coordinates of a "local" coordinate space, and translates them into the "global" or visible coordinate space.

var mc:MovieClip = new MovieClip();
var t:TextField = new TextField();

t.y = -100;
mc.y = 100;

addChild(mc);
mc.addChild(t);

trace(mc.y); // 100
trace(t.y); // -100

var localCoords:Point = new Point(t.x, t.y);
var globalCoords:Point = t.parent.localToGlobal(localCoords);

trace(globalCoords.y); // 0

(Note the use of the t.parent in calling localToGlobal; this is because t is being positioned in it's parent's coordinate space so it's from that space you want a global value)

Registration points in Flash Player are the 0,0 location of an object's own coordinate space. This point is always x=0, y=0. getRect/getBounds have no bearing on this point, but can be used to see how an object is being rendered with respect to it's registration point. If you're just trying to figure out where an object has moved when changing parents, you would want to use localToGlobal with its position and not use getRect/getBounds since getRect/getBounds values vary depending on the coordinate space in which they are calculated (though have their own uses for that purpose).

ASWC
10-24-2009, 08:49 PM
Alright so can either of you provide me with a code I can use to replace the method I use in the reflection class to find and reproduce the registration point of the object being reflected? Even though what I use works just fine it would be interesting to see another way of doing that. It's one thing to affirm that's not the right way to do it, better would be to provide examples.

senocular
10-24-2009, 10:44 PM
I don't know what you're reflection class is doing, but I imagine it's not doing what we're talking about here. This is simply about determining repositioning within display list hierarchies. I don't doubt that your reflection class probably requires the use of getRect/getBounds.