views:

21

answers:

2

Hi all, Where does the .x and .y property of a movieclip in actionscript 3.0 measured from? from the centre of the object? or.....?

For instance, given a pro grammatically drawn Sprite:

            graphics.beginFill(0x000000);
        graphics.moveTo(9.00000000,-7.00000000);
        graphics.lineTo(13.00000000,0.00000000);
        graphics.lineTo(9.00000000,6.00000000);
        graphics.lineTo(-11.00000000,6.00000000);
        graphics.lineTo(-14.00000000,0.00000000);
        graphics.lineTo(-11.00000000,-7.00000000);
        graphics.lineTo(9.00000000,-7.00000000);
        graphics.endFill();

Where will sprite.x and sprite.y measure from?

The top left hand corner? Or center of the sprite? or...?

Please enlighten me, thank you guys!

Best Regards.

+1  A: 

The origin is always the top left corner of the object. x grows positively towards right and negatively to left; y grows positively towards the bottom and negatively towards top.

0,0 ---- 5,0
|         |
|         |
|         |
0,5 ---- 5,5 

Thus the origin of stage/root object is top left corner of the SWF because its coordinates are 0,0. If you add a display object to the root object and set its x and y to 5, (mc.x = 5; mc.y = 5;), and draw a line on its local coordinates from 0,0 to 15,15 that line would be drawn from 5,5 to 20,20 on the global coordinates.

Check out localToGlobal and globalToLocal methods of the DisplayObject class.

Amarghosh
hi, i've tried the .x and .y property on the Sprite I have drawn, and its not on the top left hand corner. Any idea how do i derive the.x and .y coordinates?
ActionScriptNoob
@action Your code will draw a hexagon around origin. I don't understand what you mean by deriving x and y coordinates. The local coordinates have nothing to do with the x/y positions (other than when you translate them to global coordinates). Each object has their own coordinate system.
Amarghosh
A: 

I think your confusion comes from the layered nature of the coordinate systems in Flash. When you draw your Sprite, the x and y values you pass to the graphics methods (e.g. lineTo) are measured relative to the coordinate system of the sprites. Moving the sprite's .x and .y will move everything in the sprite's graphics. So, if the sprite was initially at (0,0) and ran the above code, much of the drawing is off the screen (because it draws to negative x and y values. If, after the above code was run, you moved the sprite to (14,7), all the lines would be visible (just barely).

Zev