2

I need to get the resized Width and Height of children in a resized parent. I have an image that is 1080 x 612. I put that in a MovieClip called "stackoverflowLOGO". That is the child width/height I need and it is inside a class called stackOne.

My Stage is set to 500 # 700 First I resize stackOne and check the width and height and they come out with the correct values. This can be seen in image #1. Here is the code to get these values

stackOne = new stackoverflowClass();

stackOne.width = screenX;
stackOne.scaleY = stackOne.scaleX;
addChild(stackOne);

// Width was 1080 and is now 500    
stackOne.imageWidth.text = "" + stackOne.width + ""; 
// Height was 1920 and is now 283.35
stackOne.imageHeight.text = "" + stackOne.height + "";

However when I check the height of an MovieClip inside of the parent that is scaled. The values do not change. I REALLY need to know how to get the actual size of the children inside of scaled MovieClips.

Here is the code that checks the width/height of the child inside of a scaled Movieclip. This is the part of the code that does not work.

stackTwo = new stackoverflowClass();
stackTwo.width = screenX;
stackTwo.scaleY = stackTwo.scaleX;
addChild(stackTwo);

// Was 1080 and still is 1080 even tho the size has been scaled.
stackTwo.imageWidth.text = "" + stackTwo.stackoverflowLOGO.width + "";
// Was 1920 and still is 1920 even tho the size has been scaled.
stackTwo.imageHeight.text = "" + stackTwo.stackoverflowLOGO.height + "";

Here is a screenshot: enter image description here

I am also including the full code below. Here is a zip file of the fla and the two classes used.

Here is the full code in the MainStack Class:

    package 
    {

import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.system.Capabilities;

import stackoverflowClass;


public class MainStack extends MovieClip
{

public var screenX:int;
public var screenY:int;

public var stackOne:stackoverflowClass;
public var stackTwo:stackoverflowClass;

public function MainStack()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
screenX = stage.fullScreenWidth;
screenY = stage.fullScreenHeight;

firstStack();
secondStack();

}

public function firstStack()
{



stackOne = new stackoverflowClass();

stackOne.width = screenX;
stackOne.scaleY = stackOne.scaleX;
addChild(stackOne);

stackOne.imageWidth.text = "" + stackOne.width + "";
stackOne.imageHeight.text = "" + stackOne.height + "";
}

public function secondStack()
{


stackTwo = new stackoverflowClass();
stackTwo.width = screenX;
stackTwo.scaleY = stackTwo.scaleX;
addChild(stackTwo);


stackTwo.imageWidth.text = "" + stackTwo.stackoverflowLOGO.width + "";
stackTwo.imageHeight.text = "" + stackTwo.stackoverflowLOGO.height + "";
stackTwo.y = stackOne.height;
}
}

}
Papa De Beau
  • 3,744
  • 18
  • 79
  • 137

2 Answers2

2

If you're using scaleX and scaleY to scale your movieclip, height and width will report the original measurements. I ran into the same problem when developing mobile applications.

I have a big movieclip that basically contains all my game elements, and I rescaled this movieclip with a rescale factor computed based on the device's resolution. To get the actual height of the big movieclip I did something like:

var actualHeight:Number = bigMovieClip.height * RESCALE_FACTOR;

EDIT: RESCALE_FACTOR is something I computed in the resize event for the stage which is going to be fired when opening the application. Here are some snippets of the code I used for a mobile game:

private static const ORIGINAL_WIDTH:Number = 960;
private static const ORIGINAL_HEIGHT:Number = 540;  

[...]

private function resizeAndCenter(evt:Event):void{
  stage.removeEventListener(Event.RESIZE, resizeAndCenter); 

  var guiSize:Rectangle = new Rectangle(0, 0, ORIGINAL_WIDTH, ORIGINAL_HEIGHT); 
  var deviceSize:Rectangle = new Rectangle(0, 0, Math.max(stage.fullScreenWidth, stage.fullScreenHeight), Math.min(stage.fullScreenWidth, stage.fullScreenHeight)); 

  // if device is wider than GUI's aspect ratio, height determines scale                        
  if ((deviceSize.width/deviceSize.height) > (guiSize.width/guiSize.height)) { 
      appScale = deviceSize.height / guiSize.height;                            
  } // if device is taller than GUI's aspect ratio, width determines scale 
  else { 
      appScale = deviceSize.width / guiSize.width;                              
  } 

  RescaleFactors.RESCALE_FACTOR = appScale;

}           

ORIGINAL_WIDTH, ORIGINAL_HEIGHT are the dimensions of the big movieclip with all the game elements. When this movieclip is added to stage is scaled with this RESCALE_FACTOR, something like:

 this.scaleX = RESCALE_FACTOR;
 this.scaleY = RESCALE_FACTOR;

And to get the actual width or height of the children, you just multiply their dimensions with this rescale factor. I hope it's more clear now.

  • Thanks Christina, this sounds very interesting. I have not tried it yet but I am wondering if RESCALE_FACTOR is a function or it's something I have to write to get my own vars. Thanks so much for your input. This is exactly what I am dealing with, a mobile app and the screen rez on the iphone. – Papa De Beau May 11 '14 at 23:47
  • Even though you are somehow right about the mobile technology, I don't find this any how connected to his problem.. And I think it's misleading. Anyways the idea of mobile development is similar to yours. – Andrey Popov May 19 '14 at 13:25
  • I think in the original question he mentioned he was developing for mobile devices, but apparently left that out at a later edit. – Cristina Georgescu May 19 '14 at 13:41
2

Now everything's clear - you want to know the size of a DisplayObject INSIDE a scaled DisplayObject :)

So it's very simple solution.. scaleX and scaleY of the parent are your scale factor, that you must work with the clips inside. Check this image:

Stack Overflow parent scale problem

First on the left is my regular setup - movie clip (named main), 100x100, with another movie clip inside (named second).

I've duplicated this, and set scaleX and scaleY to be 2. I've traced the width before and after modification - it's 100, then it's 200.

As you can see, the internal movie clip (second) is twice as big, BUT it's width is again 50. This is normal, because in it's own space, it's still that big.

Then I've simply divided it's width by the scale of the parent (/= is actually scaleX = scaleX / main.scaleX).

Then the second square remains exactly the same size, AND it's width it changed to 25, because it's divided by 2 - the scaleX of the parent.

So you should start working with parent's scaleX and scaleY and divide or multiply everything - positions, size, everything.

If you need to know the actual display width of the child (second), then just use:

trace (main2.second.width * main2.scaleX); // child's width * parent's scaleX
  • .width will give you the REGULAR, unchanged width
  • .width * scaleX (MULTIPLY) will give you the actual VISIBLE width (on screen)
  • .width / scaleX (DIVIDE) will discard the scaling and give you the new value with which you can keep the child with the same visible width

Hope that clears it out.

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • So the var scaleFactor will give me the actual height in pixels of the newly scaled clip? Cause this is what I need. Because it's different on iPhone than my Android device. So when I check the normal height of the scaled object both are different. And nope, not kidding as I said in my question: "This issue has plauged me for years. I have not found a solid answer yet and always have to use a work around. I would LOVE to know how to do this and use it in all my code in future projects as well." – Papa De Beau May 18 '14 at 14:43
  • I truly don't understand your problem now. If you scale something and then ask for it's height, it WILL give you the new height. I don't understand what's different on those devices.. The right thing to do is to set a default stage width and height that you are working with. Let's say 320x480. When your app starts, you calculate the difference between the current stage and the default one. Let's say 640x960. Your scale factor is 2. So everything should be twice... – Andrey Popov May 18 '14 at 18:41
  • My default stage is 1080 X 1920. Yeah I don't know why but when I resize for a smaller screen on iPhone and some other DROID devices it shows the same size as the original pixels. :( perhaps it's a timing issue, meaning it not yet calculated and giving the original size and not the new size. – Papa De Beau May 18 '14 at 18:51
  • scaleX and scaleY are synchronous methods, which means that on the very next line you have the new size. Provide some samples that you say are not working. I provided one, and it's working like a charm :) You're doing something wrong I guess.. – Andrey Popov May 18 '14 at 18:53
  • OK later tonight I will do some simple code and take screen shots of the output size on both iPhone and droid. I think Cristina was on to something. Check out her answer. – Papa De Beau May 18 '14 at 18:58
  • Ok I totally changed my question as I needed the size of children in an MC. See my very edited question. :) I included the full code and an fla so you can see the issue on your computer in you like. :) – Papa De Beau May 19 '14 at 07:42
  • wow, Thank you so much Andrey. This was a great explanation. I hope it was worth 50 pts. :) I hope to it saves me hours and days of pain in the future. Again thanks so much! Keep AS3-ing on! :) – Papa De Beau May 19 '14 at 16:33
  • 1
    It's not about the bounty, nor the scores. It's about helping people dealing with such problems. I personally know that sometimes you just need a little push to understand the logic, and it saves days if someone tries to help you. Cheers mate, I'm glad I could help! :) btw, the bounty is manually awarded, just to know (I don't care about points) – Andrey Popov May 19 '14 at 16:49
  • Wow, you are the man. Thanks for the great attitude. So how do I manually award you the points? I will look into it right now but maybe you can send me in the right direction. – Papa De Beau May 19 '14 at 16:52
  • I figured it out. :) It says I can't give the award for another 14 hours. – Papa De Beau May 19 '14 at 16:52
  • DUDE! You have no idea how happy I am right now. This has changed my LIFE! :) Now in peace I can do calls like this and save hours and hours and hours of pain. And it works flawlessly! --- theStats.y = menuBanner.height * parent.scaleY; = WOW! Speechless! Thank you Andrey! – Papa De Beau May 19 '14 at 17:29