Groups | Blog | Home
all groups > flash actionscript > february 2007 >

flash actionscript : [Flash 8 Actionscript] _x property of dynamically created movieclip


Johnny Rodgers
2/28/2007 9:22:47 PM
Hi everyone.

I'm having trouble with how Flash sets the _x property of a dynamically
created movieclip.

If I create a rectangle on the stage using the Drawing API:

Code:

//for the purposes of this example, assign a value to i
var i:Number = 0;

//assign var squareName
var squareName:String = "square" + i;

//create an empty movieclip named [squareName] at depth 1
this.createEmptyMovieClip([squareName], 1);

//draw a 100 x 100 pixel rectangle originating at coordinates (100, 100)
this[squareName].beginFill("0xFFFFFF", 100);
this[squareName].moveTo(100, 100);
this[squareName].lineTo(200, 100);
this[squareName].lineTo(200, 200);
this[squareName].lineTo(100, 200);
this[squareName].lineTo(100, 100);
this[squareName].endFill(); // stop the fill

//trace the _x property of this[squareName]
trace("this[squareName]._x: " + this[squareName]._x); //RETURNS 0: shouldn't
this be 100?

The _x property of the dynamically created movieclip is relative to itself
(ie. 0...the upper left corner of the movieclip). I know I can get the global
_x value of the movieclip by creating a bounds object and calling
targetBounds.xMin using something like this:

Code:

//trace the global x coordinate for the movieclip
var targetBounds:Object = this[squareName].getBounds(_root);
trace ("targetBounds.xMin: " + targetBounds.xMin);

But how do I create a movieclip so that it's _x property will be relative to
the Stage in the first place, and not itself? It's important, because if I then
want to move the movieclip using something like this:

Code:

this[squareName]._x = 200;
trace("this[squareName]._x: " + this[squareName]._x); //RETURNS 200: shouldn't
this be 300?

Then the square moves 200 to the right within itself. This is clearly not what
I want to do. I want to set the value of _x to the coordinate 200 (not 300, as
this does). I have tried doing the same using the setProperty function:

Code:

setProperty(this[squareName], _x, 200);

But with the same result. This example code is highly simplified from the
working file, but shows the problem.

I'm not new to Flash by any stretch of the imagination, but I have never come
up against this problem before. For context, I'm actually getting my feet wet
with the Tween class, and am having trouble animating dynamically created
movieclips due to this confusion with coordinates. When I tween a dynamically
created clip from one set of coordinates to another, they are offset by the
movieclip's _x and _y properties starting at 0, 0, rather than their actual
position on stage.

ANY help is greatly appreciated, as I've been fighting with this for hours.

Cheers! JR.[h]Text[/h][b]Text[/b]
NoNickNeeded
2/28/2007 10:35:42 PM
I'm thinking that your movieclip gets placed at 0,0. Then, you create a
rectangle INSIDE of that movie clip, at 100,100. So, tracing the _x value of
the movieclip, 0 would be the correct value. Changing _x to 200 will move the
movieclip 200 pixels to the right. Visually, it would appear as though the
rectangle is now 300 pixels from the left side of the stage - 200 for the
parent movieclip, plus another 100 based on where you started the rectangle.

You could just start the rectangle at 0,0 inside the movieclip, then move the
clip to where ever you wanted.
AddThis Social Bookmark Button