Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Flash use the entire height of a masked object instead of just the visible area?

The problem is if I set the .mask property of a Sprite object, the Sprite object still returns it's full height when I call Sprite.height. So I figured a simple way to overcome this would be to override the property getter.

Now.. even though that worked, if I add this masked Sprite object to another Sprite object, the new Sprite object will report it's height as the masked Sprite object's height even though I have overridden the property to return only the height of the visible area due to the mask. So it seems Flash ignores the fact that not all of the content is visible but still automatically increases the new Sprite object's height as if there was no mask on the masked Sprite.

So I am wondering if there is a workaround so I can add this masked object to any other DisplayObject knowing it will be resized to only what is visible in the masked object.

Thanks for any help.

EDIT

Here is a code example..

var content:Bitmap = new Bitmap(new BitmapData(50, 100, false, 0x000000));
var container:Sprite = new Sprite();
var mask:Bitmap = new Bitmap(new BitmapData(50, 50, false, 0x000000));

container.mask = mask;

container.addChild(content);

trace(container.height) // this should return 50 instead of 100
like image 375
xLite Avatar asked Jul 25 '11 18:07

xLite


1 Answers

So for those interested I managed to come up with a workaround. What really annoys me is that DisplayObject.scrollRect doesn't update the dimensions immediately so until Adobe get off their arse and fix it, here's what you need to do.

The problem with .scrollRect, as I've mentioned, is that it doesn't adjust to the new width and height until after a frame has passed or after a certain amount of time. Either way I've basically built my class so that once .scrollRect is set, I create a timer that runs every millisecond, checking if .width and .height has updated to the new values. Once it has, I dispatch a custom READY event which the parent object listens out for before adding the class to the stage.

Now one very important thing you need to know about this arsehole of a property is that the object of which you are setting the .scrollRect property must be added to the stage before it makes any attempt at updating it's width and height values to those set in the .scrollRect property. So to do this as clean as possible, I pass the Stage object from my main class to the object whose .scrollRect I want to adjust, let's call it MyClass. MyClass sets its .visible property to false then adds itself to the stage. Only once this is done can I now begin my 1ms timer to check if the dimensions have changed correctly after I have set .scrollRect.

So once the timer tick handling function sees the dimensions have changed successfully, MyClass can now dispatch the MyClass.READY event to the main class while at the same time, setting its .visible property to true and removing itself from the stage. When the main class's event listener is triggered, it can now add MyClass to wherever it wants to, knowing that it will have the correct dimensions as set in MyClass.scrollRect.

This has been a great pain to myself and probably many other developers looking for solutions so I hope this has helped you and will suffice for now until Adobe does something about this. Thanks to everyone who posted previously who tried to help, I really appreciate it, thanks!

like image 158
xLite Avatar answered Sep 30 '22 13:09

xLite