MovieClip resizes, but its children's height and width are not changed? - actionscript-3

Changing the width and height of the parent MovieClip does not bring change in the width and height of the inner MovieClip. The parent MovieClip is placed at Stage and is resized manually. When I assign the dimension of the parent MovieClip to the inner MovieClip through code, the parent MovieClip dimension is changed. I want both MovieClip to be of same width and height at runtime. However, parent MovieClip dimension is changed at design time by me.
Example:
There are two MovieClip, one inside another. Now parent MovieClip is placed at Stage at design time and its dimension is (50,50) and the child MovieClip which is inside the parent MovieClip has also same dimensions (50,50). Now, I manually change the parent MovieClip dimension by pressing Q and stretching it with mouse, the dimension of the parent MovieClip is now (100,150) or whatever I like. Now double-click on parent MovieClip and check that inner MovieClip dimension remains same i.e. (50,50)
Now in AS3 code, I change the width and height of inner MovieClip like this:
saveheight = parentmc.height;
savewidth = parentmc.width;
now I change the child MovieClip according to the dimensions of the parent MovieClip like this:
parentmc.inner_mc.width = parentmc.width;
parentmc.inner_mc.height = parentmc.height;
but this brings change in parentmc also so I reassign value to parentmc like this:
parentmc.height = saveheight;
parentmc.width = savewidth;
In above case, parentmc and inne_rmc dimension should be same i.e (100 ,150). With swapping the values as above, I get parentmc and inner_mc to be of same dimension, but object size is never (100, 150), I have checked it with pixel-perfect air app.

In your code, you are neglecting to account for the transformation to the parent that you did with the 'Q' tool in the authoring environment. The childmc's width and height are expressed in terms of parentmc's transformed coordinate space. If you wish to scale the inner clip to a specific size in stage coordinate space you need to account for the scale of the parent that resulted from your transform:
parentmc.inner_mc.width = parentmc.width/parentmc.scaleX;
parentmc.inner_mc.height= parentmc.height/parentmc.scaleY;
Also, if the clips aren't aligned (e.g. registered by their upper left corner and with the child at 0,0), enlarging the child could push out the boundaries of the parent.
You can also use the parent's transform matrix if you prefer that over using scaleX and scaleY.
UPDATE 4.8.11: Were you perhaps asking to do this (runtime removal of the authoring-time transform)?
saveheight = parentmc.height;
savewidth = parentmc.width;
parentmc.scaleX = 1.0; // Undo authoring-time scale transform
parentmc.scaleY = 1.0; // Undo authoring-time scale transform
parentmc.inner_mc.width = savewidth;
parentmc.inner_mc.height = saveheight;
parentmc.width = savewidth;
parentmc.height = saveheight;
Note: I'm not at a computer set up with Flash to test this, so please leave me a comment if this does not do what you are expecting, and I will happily check my work and follow up.

it doesn't quite work like that,
you need to multiply the children by the scale of the parent.
the other thing you can do is use getBounds and then you can get the bounding rectangle of any child (and child's children etc) relative to the parent

I honestly don't understand what you're asking, because what you describe should work!
If we have a parentMC and a childMC both with a 50 height and 50 width, if you change the parentMC.scaleX = 2; it will apear to be 100 in width. The same goes for the childMC.
Could you please provide an example of what you're trying to do here? - or some code.

Related

Scale, Position & Rotate Parent object to make child object take up entire stage

Using the first photo below, let's say:
The red outline is the stage bounds
The gray box is a Sprite on the stage.
The green box is a child of the gray box and has a rotation set.
both display object are anchored at the top-left corner (0,0).
I'd like to rotate, scale, and position the gray box, so the green box fills the stage bounds (the green box and stage have the same aspect ratio).
I can negate the rotation easily enough
parent.rotation = -child.rotation
But the scale and position are proving tricky (because of the rotation). I could use some assistance with the Math involved to calculate the scale and position.
This is what I had tried but didn't produce the results I expected:
gray.scaleX = stage.stageWidth / green.width;
gray.scaleY = gray.scaleX;
gray.x = -green.x;
gray.y = -green.y;
gray.rotation = -green.rotation;
I'm not terribly experienced with Transformation matrices but assume I will need to go that route.
Here is an .fla sample what I'm working with:
SampleFile
You can use this answer: https://stackoverflow.com/a/15789937/1627055 to get some basics. First, you are in need to rotate around the top left corner of the green rectangle, so you use green.x and green.y as center point coordinates. But in between you also need to scale the gray rectangle so that the green rectangle's dimensions get equal to stage. With uniform scaling you don't have to worry about distortion, because if a gray rectangle is scaled uniformly, then a green rectangle will remain a rectangle. If the green rectangle's aspect ratio will be different than what you want it to be, you'd better scale the green rectangle prior to performing this trick. So, you need to first transpose the matrix to offset the center point, then you need to add rotation and scale, then you need to transpose it away. Try this set of code:
var green:Sprite; // your green rect. The code is executed within gray rect
var gr:Number=green.rotation*Math.PI/180; // radians
var gs:Number=stage.stageWidth/green.width; // get scale ratio
var alreadyTurned:Boolean; // if we have already applied the rotation+scale
function turn():void {
if (alreadyTurned) return;
var mat:flash.geom.Matrix=this.transform.matrix;
mat.scale(gs,gs);
mat.translate(-gs*green.x,-gs*green.y);
mat.rotate(-1*gr);
this.transform.matrix=mat;
alreadyTurned=true;
}
Sorry, didn't have time to test, so errors might exist. If yes, try swapping scale, translate and rotate, you pretty much need this set of operations to make it work.
For posterity, here is what I ended up using. I create a sprite/movieClip inside the child (green) box and gave it an instance name of "innerObj" (making it the actually content).
var tmpRectangle:Rectangle = new Rectangle(greenChild.x, greenChild.y, greenChild.innerObj.width * greenChild.scaleX, greenChild.innerObj.height * greenChild.scaleY);
//temporary reset
grayParent.transform.matrix = new Matrix();
var gs:Number=stage.stageHeight/(tmpRectangle.height); // get scale ratio
var mat:Matrix=grayParent.transform.matrix;
mat.scale(gs,gs);
mat.translate(-gs * tmpRectangle.x, -gs * tmpRectangle.y);
mat.rotate( -greenChild.rotation * Math.PI / 180);
grayParent.transform.matrix = mat;
If the registration point of the green box is at one of it's corners (let's say top left), and in order to be displayed this way it has a rotation increased, then the solution is very simple: apply this rotation with negative sign to the parent (if it's 56, add -56 to parent's). This way the child will be with rotation 0 and parent -> -56;
But if there is no rotation applied to the green box, there is almost no solution to your problem, because of wrong registration point. There is no true way to actually determine if the box is somehow rotated or not. And this is why - imagine you have rotated the green box at 90 degrees, but changed it's registration point and thus it has no property for rotation. How could the script understand that this is not it's normal position, but it's flipped? Even if you get the bounds, you will see that it's a regular rectangle, but nobody know which side is it's regular positioned one.
So the short answer is - make the registration point properly, and use rotation in order to display it like in the first image. Then add negative rotation to the parent, and its all good :)
Edit:
I'm uploading an image so I can explain my idea better:
 
As you can see, I've created a green object inside the grey one, and the graphics INSIDE are rotated. The green object itself, has rotation of 0, and origin point - top left.
#Vesper - I don't think that the matrix will fix anything in this situation (remember that the green object has rotation of 0).
Otherwise I agree, that the matrix will do a pretty job, but there are many ways to do it :)

ActionScript 3: Zoom into movieclip while not scaling its childrens

I've included a zoom functionality similar to the one explained at this website:
http://www.flashandmath.com/howtos/zoom/
This works perfectly on my background image(a map, that is), but I want to keep the symbols on my map the same size while zooming in.
I probably could work this out by changing all the children's size when calling the zoom-function, but I am hoping there is some kind of easy code adapt in my children class to make the size of the instances unchangable. Is there?
Thanks!
One crude way, so you don't have to calculate the symbols scale, would be to remove the symbols from the mapDisplayObject so they're no longer a child and instead put symbol placeholders. Then match each symbol's x and y to each place holder, using localToGlobal...
If your children are not scaled or skewed or rotated you can iterate all of them and set transformation matrix to 1/parentScale. Something like:
for each (var child:DisplayObject in parent) {
var matrix:Matrix = child.transform.matrix;
matrix.a = 1/parentScale;
matrix.d = 1/parentScale;
child.transform.matrix = marix;
}

Getting graphic/movie clip x,y position from within another movieclip?

This should be fairly simple I'd think, I'm just not that familiar with actionscript haha.
I have a game where I have the background moving behind a character that stays in one position on screen. I'm relatively new to actionscript 3 but I'm wanting to have text boxes pop up whenever the player presses a key over certain objects passing in the background.
So, basically the background itself is a movie clip, and I have other graphics and movie clips within the background mc.
I was thinking of getting the player.x and y position and then "comparing" that position (>= and <=, etc.) with the graphic/movie clip in the background. But I just don't know how to obtain the x and y coordinate of the graphics/movie clips in the background mc.
You could try to target your movie clips in the background by getting their coordinates, then removing their parent's position (the background container).
Something like :
var finalXPosition:int = targetMovieClip.x - backgroundContainer.x;
var finalYPosition:int = targetMovieClip.y - backgroundContainer.y;
By substracting the target movieclip parent's position to its position, you gain the final position in the parent's scope coordinates.
It should work for you as soon as your character and your background container are situated at the same level of the display list.
Here is a quick diagram of what I try to explain (please forgive my inaptitude to draw nice and explicit drawings ^^)
Usually, when I stumble upon such a case, I try to make a quick and even dirty drawing, starting with what I want, then breaking down every useful data I have to achieve that result, you should keep that method in mind and try it the next time ! :-)

adding transparent sprite over another sprite

I need to place a transparent sprite over another sprite. The overlaying sprite will acept some mouse events. When a user move mouse over upper sprite a curve will be drawn. After it'll be processed it will be drawn on the base sprite (and erased on upper).
The idea I have now is to place the sprite, draw a rectange of size equal to sizes of sprite and set alpha to 0.
The question is a bit dump: maybe the proposed solution is not the best. Is there a better way to set width and height (as far a I understand Sprite.width = w; will not help)?
Thank you in advance!
You can't set dimensions directly, while you can draw over that Sprite. So you can do like this:
graphics.beginFill(0,0); // zero alpha fill
graphics.lineStyle(0,0,0); // invisible lines
graphics.drawRect(0,0,width,height);
graphics.endFill();
This way your Sprite can have its alpha remaining at 1, to not hide anything that's its child. Then, whatever curve you would decide to draw in that Sprite, you can draw within a child Shape object, via graphics.moveTo and graphics.lineTo.
UPDATE: According to comments below, setting alpha to 0 won't work with newer Flash player versions, so alpha should be set to a nonzero amount for the events to register on the overlapping sprite.
graphics.beginFill(0x808080,0.01); // almost zero alpha fill

as3 addChild into scaled movie clip & keep same scale and position

ATTEMPTING:
Loading a movie clip (NoScale_mc) into a scaled movie clip (Scaled_mc).
ISSUE:
When I load the movie clip NoScale_mc into Scaled_mc it obviously scales too.
QUESTION:
How can I keep the NoScale_mc in THE EXACT SAME POSITION and THE EXACT SAME SCALE but yet still load it into the Scaled_mc using the addChild() method?
You could do a little reverse tirckery with math to try to accomplish this. So get the scaled values of the parent DisplayObject and use those values to inverse-scale the child DisplayObject.
For instance, the parent DisplayObject is scaled to: scaleX = 1.45 and scaleY = 4.6. So you can set the child DisplayObject to: scaleX = 1/1.45 and scaleY = 1/4.6.
This may produce odd results though, and will most-likely end up being a headache to maintain. You're probably better off adding the child DisplayObject to the stage on top of the parent, like Marty Wallace said. If you want to keep it looking aligned with the parent DisplayObject, then just set both of their x and y positions to the same thing (or with an offset, if that is what is desired).