Normally, to center an instance on the stage to another vertically horizontally, you can use simple lo开发者_运维问答gic and by referencing width, height, x and y co-ordinates of the symbols involved.
This seems impossible in-situ, because Textfield.height and Textfield.textHeight report incorrect values. They bring back the same values, which don't reflect the actual height of the instance on the stage.
I have crafted this simple example to illustrate the problem: http://omnom.replete.nu/fails.fla
Thanks.
I might have misunderstood your question, but if not, did you try this one?
red.tf.y = red.blue.height / 2 - red.tf.height / 2 + red.blue.y;
This is centering the textfield (tf) to the blue box vertically.
Basically you get the center y coordinate of the blue rectangle, subtract the center y coordinate of the textfield and then finally add the blue rectangle's y position.
For the best result you need the textfield with autoSize defined, also that the movieclips have (0, 0) reference points.
It's the same horizontally, but with x coordinate and width parameter.
red.tf.x = red.blue.width / 2 - red.tf.width / 2 + red.blue.x;
The height of the TextField does not relate to the Height of the text itself.
Text height can change for a number of reasons.
A good example would be the font family and font sizes.
You need to use text metrics to get that info.
ALL THE INFO HERE
[EDIT]
per your comments the last line of your scaleTextToFitInTextField function should do this.
you need to find the center of both objects and subtract them then add the offset
txt.x = cont.width/2 - txt.width/2 + cont.x
txt.y = cont.height/2 - txt.height/2 + cont.y
[EDIT EDIT]
Also remove these 2 lines as they are now redundant
red.tf.x = red.mchammer.x;
red.tf.y = red.mchammer.y;
精彩评论