how to get the global width and height of movieClip after scaling parent(s)? - actionscript-3

I do work on some already developped project.
myMovieClip has been scaled and is nested into many movieclips which may have been scaled themselves
When I trace his width and height, it does not give me the right width and size:
How can I get the absolute width and height ?
(the width and height it takes on the screen)
(a kind of localToGlobalWidth function)
regards

You can use the getBounds method of a display object to get the bounds (actual width/height and position) relative to whatever display object you pass to the method as an argument. (doucmentation)
myScaledObj.getBounds(stage); //would return a rectangle of where on the stage the display object is.
The width and height property of the returned rectangle would be what you'd use.

Do you know how many times it's been scaled, and by how much it's being scaled? In that case, you could trace the width * the scaling, for example:
mc1 has a width of 100, and is being scaled by 2:
mc1.scaleX = 2;
trace (mc1.width*2);
Extra information: if it's being scaled by a variable, replace 2 with the variable name.
This method should work even if it's being scaled multiple times, by using a variable to pile up the scaling:
var scaler1:int = 2;
var scaler2:int = 7;
var scalePiler:int = scaler1*scaler2;
mc1.scaleX = scalePiler;
trace (mc1.width*scalePiler);
Hope I helped and covered all possibilities, good luck with your program! ^^

Related

MovieClips scale down when StageScaleMode.NO_BORDER

I have my stage aligned to TOP_LEFT and scaled to StageScaleMode.NO_BORDER, the width and height of my SWF are : width = 1920 and height = 1080.
The thing is when my i change the SWF size to bigger (height/width) the movieClips on the stage scale up and that's a good thing for me, but when i reduce the size of my SWF to smaller values (smaller than the default size), the movieClips get reduced as well, aren't they supposed to stay the same, if not is there a way to make that happen ?
In fact the NO_BORDER scaleMode makes sure to not show the borders of the SWF and scale the center depending on the :
The ratios :
widthRatio = currentStageWidth/stageWidth
and
heightRatio = currentStageHeight/stageHeight.
The stageWidth and stageHeight are provided by AS3.
The currentStageWidth and currentStageHeight are supposed to be changing while resizing the browser window, the can be retrieved from the Flash Object (ie: $("#flashObjectId").width()).
Then we should compare the two ratios (widthRatio and heightRatio), the bigger one has the scale value for the stage and the other elements on the stage.

How does Flash calculate a DisplayObject's dimensions based on the children?

I have a class called Container which extends Sprite and adds the ability to set width and height manually, with scaleX and scaleY always remaining 1.
I have put such a class into a sprite and noticed the sprite measures itself by internal calculations, not actually getting width / height from the getters.
What is this math and how can I extend it to force the Sprite to consider my extended width and height getters while doing its internal measures?
The math lies within getBoundary() function that's called for all of the children and its own graphics to determine the coordinates for the edges of the entire sprite's display list. That's why setting width without scaling might not be useful. But if you still wish to do this, first make fields that will contain info on set width and height, which you will update on container.width and container.height assignment, then you also override getters on width and height, returning maximum of stored width/height and calls of super.width and super.height.
var storedWidth:Number=0; // to not have NaNs
public override function get width():Number {
var w:Number=super.width; // get width from that math
if (storedWidth>w) w=storedWidth; // or any other logic, to operate these values
return w;
}
The same approach with height.

Easeljs, is it a recommend way to fix the size of container?

Maybe I am wrong, I learn Easeljs for a week only.
The Container has no width and height to set the size.
I have 2 questions:
1. is the size of Container, dynamically change with the child.
2. if question 1 answer is yes, can I add a big bitmap or shape to it, eg. a background image...etc. to control the size of Container?
Consider a container as a group of objects, not a physical container. Containers give you the ability to transform, translate, cache, and otherwise control multiple items as a single item. They do not really have a physical size, except that of their collective children.
There is no width or height mainly due to the cost of calculating the size, especially considering transformations, sub-containers, etc. There may be support added in the future for width/height, but for now its not available.
[UPDATE]: Containers do have bounds, based on the bounds of children (retrieved using container.getBounds() (docs) that have bounds. For example, a Container that has Sprite, Bitmap, Text, objects with manually set bounds, or cached DisplayObjects will report bounds using those children. Shapes do not have auto-calculated bounds currently, so will not contribute to container bounds.
This is a very bad hack, but if you absolutely must fix the size of the container, you can use something like this:
var blank = new createjs.Shape();
var width = 1;
var height = 400;
blank.setBounds(0, 0, width, height);
container.addChild(blank);
This will set the size of container to the blank image and now any background you draw on it will be visible.

Any way to affect the original width of the stage, in modes other then StageScaleMode.NO_SCALE?

Documentation for the stageWidth property of the stage object states that:
If the value of the Stage.scaleMode property is set to
StageScaleMode.NO_SCALE when the user resizes the window, the Stage
content maintains its defined size while the stageWidth property
changes to reflect the new width size of the screen area occupied by
the SWF file. (In the other scale modes, the stageWidth property
always reflects the original width of the SWF file.)
But is there any way to affect that original width of the stage, without recompiling swf?
The stageWidth and stageHeight can not be set. They are based on the stagescalemode and the html container.
Or lets say there is a limited huge artboard what is the displayable area in flash(not regarding stageWidth).
If your swf has StageScaleMode.NO_SCALE its stageWidth will tell you the width of its container.And it will always fit its parent html element by showing you more or less of this artboard. But this can not directly be set by as3. To do so you need js.
If you have any StageScaleMode that scales the swf. Then its getting messy because that limited huge artboard gets streched and distrorted to meet the fittings.One pixel in as3 doesnt mean a pixel on screen anymore. But the part of that artbord that is visible will always be the same.

Is there a reason I can't change the width and height of an empty display object?

In the documentation for DisplayObject, it states that the width and height of the DisplayObject can not be changed if it is empty. Why is this restriction necessary? In every other framework I have used, you can resize containers that are empty. What is the reason for this madness?
In Flash, a DisplayObject is not solely a container for other DisplayObjects, though it can be. In Flash, a DisplayObject is just that, a displayable object... one that can be added to the DisplayList. What would be the original size of an empty sprite? 0x0? 1x1? The size is therefore determined by the contents being displayed.
That being said, you can change the scaleX and scaleY properties of an empty DisplayObject.
var c:Sprite = new Sprite();
c.scaleX = c.scaleY = 2;
c.graphics.beginFill(0);
c.graphics.drawRect(0, 0, 200, 80);
c.graphics.endFill();
addChild(c);
The sprite is 400x160 on the stage. :)
The reason you can't change an empty object's width and height is that they are ultimately derived properties. Along with the object's content, scaleX and scaleY are what really determines the object's size - width and height are really just convenient ways to adjust the scale.
In other words, when you change an object's width property, all Flash really does is to calculate what scaleX would be necessary to achieve the desired width, and then change scaleX to that value. When the object is empty, there is no scale value that would achieve any width except zero, so Flash ignores the request.