how can i add an event listener to detect when the height of a DisplayObject changes.
i have a holding Sprite with a border that needs to resize when any object inside changes height or is added.
thanks.
Josh
how can i add an event listener to detect when the height of a DisplayObject changes.
i have a holding Sprite with a border that needs to resize when any object inside changes height or is added.
thanks.
Josh
displayObject.addEventListener(ResizeEvent.RESIZE, onResize);
function onResize(event:ResizeEvent):void
{
// handle it here
}
Create the custom event class:
package com
{
class ChildResizeEvent extends Event
{
public static var RESIZE:String = "resize";
public static var ADDED:String = "added";
}
}
In main Sprite MXML you can use this:
<mx:Metadata>
[Event(name="onChildResize", type="com.ChildResizeEvent")]
</mx:Metadata>
Then when the event happens (When an object changes height or is added), you dispatch:
function onInteriorObjectHeightChange(event:Event):void
{
dispatchEvent(new ChildResizeEvent(ChildResizeEvent.RESIZE));
}
i presume this is a flash and not a Flex question ... which is why mx.events.ResizeEvent
(which is dispatched by UIComponent and subclasses) and all this kind of funky Flex stuff won't work ... if you do use Flex and UIComponents, it's the best way to go though ...
the problem is, that this event is not generated ...
Graphics
is final
... except, overriding the getter and setting a timeout, when the getter is used, to check changes 1 msec later (and be sure you have 1 timeout at max) ...this is more than dodgy, takes a lot of developement and debugging time, will be a pain in the ass, since you will have to make absolutely sure, everything (also any library symbol, if you use CS3/CS4 as i presume) ... and will eat up quite a chunk of performance due to all the events dispatched by your custom accessors ...
the most simple thing really, is to watch the width/height on enterframe and if they change from one frame to another, then redraw your border ... this makes much more sense, since you don't need to redraw the border more then once a frame, which is an effect that could very well occur, if you tried to capture any actions that could possibly mean resizing ... and really, comparing two floats is very cheap ... :)
hope that helped ...
At first I was surprised the implementation of the DisplayObject class does not dispatch Event.RESIZE events when its width and height properties are changed or the size of the contents is recalculated, but it makes a lot of sense for performance reasons.
I thought about using the Event.RENDER event, but I was listening for it on a TextField that I was resizing like crazy through a script... and despite the text re-wrapping, and the box expanding and the cursor flashing... it NEVER fired a single RENDER event. Surprising, it only fires the RENDER event when the text is changed, which just shows how useless the RENDER event is.
I suggest that you either:
Logically, the method you choose should depend on what you know about the DisplayObject's likelihood to change size: