Reputation:
I have problem when dealing with transform in Flash cs4 ....
When I rotate my movieclip and trace the output, my movieclip's width also changed....
Upvotes: 1
Views: 1152
Reputation: 11
I realise this is a pretty old post, so my apologies if this is breaking forum etiquette on necroposting or what ever. However, this was the first result on a google search for a fairly trivial problem and does not really seem to offer a decent way of retrieving the unrotated width or height of a display object.
The problem is that by asking for width and height, you seem to be getting the global bounds of the object, when what you want are the local bounds, this is how you do that: (just copy and paste the whole thing into a flash file and compile to see what I mean).
import flash.display.Sprite;
var spr=new Sprite()
spr.graphics.beginFill(0xff0000);
spr.graphics.drawRect(-25,-100,50,200);
addChild(spr);
spr.y=150
spr.x=300
function myOEF(e)
{
spr.rotation++
trace(spr.width,spr.height);
var bounds=spr.getBounds(this)
trace("A: Global bounds: "+bounds);
var bounds=spr.getBounds(spr)
trace("B: Local bounds: "+bounds);
}
addEventListener("enterFrame",myOEF);
Upvotes: 1
Reputation: 394
OK .. MY BAD.. I've seen my error eventually :). I was convinced this was a bug before. Though Branden was right, I still find it pretty confusing. This is eventually an example to show that Branden was right and there is no discrepancy between the last 2 values.
var box = new Sprite();
box.graphics.beginFill(0xFF0000);
box.graphics.drawRect(0, 0, 100, 100);
this.addChild(box);
trace(box.width);
box.rotation = 45;
trace(box.width);
box.width = 141.4;
trace(box.width);
Edit: You'd still might want to check PiPeep's link form grantskinner describing something like I wanted to prove above, but successuly (in Flash CS3 - AS3) - http://www.gskinner.com/blog/archives/2007/08/annoying_as3_bu.html
Upvotes: 1
Reputation: 27045
width/height will change as you rotate the clip. if you don't want this behaviour, use the scaleX/scaleY properties instead.
Upvotes: 0
Reputation: 2046
This is a bug in flash player. There are a few things you can do:
Option 1 (probably the best way):
Create a subclass of MovieClip and override the get and set methods for width and height. Have the setters call super, and store the values as private variables (eg _width/_height) When get is called, return your private variables. Since you are using a matrix, override the get and set matrix functions, set the scaling factors with your new functions, and set them to not scale before calling super. Why this works: The setters are not affected by this bug.
Option 2:
Use scaleX/scaleY instead for getting the width/height, and multiply by the width and height values for 0 rotation, 1.0 scale. Why this works: The scaleX/scaleY are not affected by this bug.
Happy Coding!
Upvotes: 1
Reputation: 4468
I'm not sure why this is a problem - it's expected behavior. The width of a clip is the actual number of pixels from the left most edge to the right most edge - which will change if a clip is rotated. If your clip has a sub-clip within it, that clip's width will not change, since presumably it's rotation isn't changing.
Upvotes: 0