How to position a child locally after the parent has rotation changed?


I want to be able to add a child to a parent that has rotation. So when I add a child to the parent I want it to be in the position as the parent has no scaling and rotation changed therefor the child obeys the transformations properly.

Maybe I am supposed to get the transformation matrix for the parent multiply it with the child’s then add it? However I just want a less cpu intensive solution if possible.

I have to add the child to the parent this way because the parent has to have some rotation first. Maybe I can add the child but not show it? How is it done? Transparency sounds like a lame way to do it.

If you don’t want it to inherit transformations maybe you don’t have to add it as a child?

You can also use display object’s globalToLocal function. It would look like that:

myParent.rotation = 70;
var globalPos:Point = myParent.globalToLocal(new Point(myParent.x + 80, myParent.y));
child.x = globalPos.x;
child.y = globalPos.y; 

Child would be positioned 80 pixels to the right of myParent.

You can use child.visible = false; to hide object. It won’t be rendered at all then.

No I want it to inherit the trasnformation so it is positioned properly.

If you use OpenFL’s display object system, the child will automatically inherit the parent’s transformations.


I think that is what I am using, not sure about what you mean. I am using Sprite and another Sprite as a child. But the issue is that I am adding the child after the constructor initialized. So it happens somewhere after the the object is initialized and some rotation applied.

It doesn’t matter what order you do things in. Take this as an example:

var parentSprite:Sprite = new Sprite();
parentSprite.x = 200;
parentSprite.rotation = 90;

var childSprite:Sprite = new Sprite();;, 0, 10);
childSprite.x = 100;

childSprite will appear at (200, 100) onscreen. Even if you re-order the code, it’ll still end up at (200, 100).

Take a look at the example of globalToLocal in my reply at the top. You can get past parent’s rotation like that, but if you change rotation of parent you will have to calculate position again.

After you said that it should inherit the rotations, I looked at how it works. Actually what happens is this, I was using the width and the height to measure in local space but the thing is that it seems like the w and h changes as the rotation changes, I am guessing that w and h are calculated with the axis aligned bbox.
So adding additional variables and assinging them to w and h in the constructor does the trick for me.

I am not sure if this is a feature or a bug.