4 Replies Latest reply on Oct 15, 2007 12:50 PM by kglad

# centered placing of a mc onto another via action script

Hello,

i have problems to place a mc object (any rectangle) centered onto another mc object (any rectangle) via as-code. Via the polar() method it is possible to translate points with a certain distance and angle. I tried with that method to place the first rectangle centered onto the second rectangle. It works fine for an angle of 0 degrees for the second rectangle. But if you change the angle of the second rectangle the determination is no more correct.

Can someone someone help me?

You just have to create 2 rectangles as movieclips in a flash document and name them as "draggedObject" and "dropObjects". Then attach the following as-code and try out:

draggedObject.x = dropObjects.x; // draggedObject is the red rectangle, dropObjects the blue rectangle
draggedObject.y = dropObjects.y;
var angle1 = dropObjects.rotation;
angle1 = 2 * Math.PI * (angle1 / 360);// angle to Bogenmass
draggedObject.rotation = dropObjects.rotation;

var newPoint = Point.polar(dropObjects.width/2 - draggedObject.width/2, angle1);
draggedObject.x = draggedObject.x + newPoint.x;
draggedObject.y = draggedObject.y + newPoint.y;
• ###### 1. Re: centered placing of a mc onto another via action script
which is the first and which is the second rectangle? and what are you trying to do?
• ###### 2. Re: centered placing of a mc onto another via action script
The draggedObject is the rectangle in the foreground which should be positioned in the center of the second rectangle (background). This should work for any position and angle of the second rectangle.

If it would be possible to position MC objects with the center as reference point for x and y coordinates it would be possible. So maybe someone has a solution for that.

Otherwise it seems to be quite hard to implement the correct mathematical formulas.

• ###### 3. Re: centered placing of a mc onto another via action script
well, there are several ways to align objects on the stage.

1) construct the objects so that thier registration points are in the center of the object, then either can be aligned to the center 0,0 of the other, and rotated about that point.

2) if the objects registration point is in the upper left corner, use the Stage's width and height properties to align the obects to the center, by setting the _x position of the object by subtracting the width of the object from the width of the Stage and dividing the result by two. do this again each time you rotate an object, as the bounding box is changed to accomidate the entire bounds of the shape, after rotation occurs.

AS2: draggedObject._x += (Stage.width - draggedObject._width) / 2;
AS3: draggedObject.x += (stageWidth - draggedObject.width) / 2;

(I am uncertain if this is the correct method of calling of the AS3 'stageWidth' property here, as I'm new to AS3)

3) there are more ways to accomplish this, but this should be enough for now.
• ###### 4. Re: centered placing of a mc onto another via action script
use the getBounds() method of the displayobject class to obtain a rectangle that you then use to center your rotated movieclip.