Hi everyone. Long time listener, first time caller here. :}
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:
//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:
//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:
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:
setProperty(this[squareName], _x, 200);
But with the same result. This example code is highly simplified from the working file, but shows the problem. It’s also attached as an .fla.
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.