views:

28

answers:

1

Hi,

I'm writing a sort of "dynamic gallery" in flash. The problem is that a child of the gallery can be resized in runtime, and then I have to rearrange the gallery.

Now, of course, I can't overload the gallery items, since it's a display object which is unpredictable. and even if I could force overloading on the items, how will I be notified if, let's say, an animation started, something move, and the item got bigger..

Is there some sort of Event for a display object which is dispatched on ANY kind of resize ??

My dumb solution was to go through all the gallery's items and rearrange them every given milliseconds, and now I feel so dirty and I need to confess my sins.

+1  A: 

Well you can tackle this in two ways, either the child object dispatches an event when its size changes or you keep listening to any changes in size with an EnterFrame event, the latter being closer to your "dirty" solution :) personally i usually go for the first solution but this is not to say that the second solution is bad, it's a matter of choice.

  • Is there some sort of Event for a display object which is dispatched on ANY kind of resize ??

You can create your own.

package events
{
import flash.events.Event;

public class SizeEvent extends Event
{
    public static const CHANGE:String = "Size Changed";
    public var params:Object;
    public function SizeEvent(type:String, params:Object)
    {
        super(type);
        this.params = params;
    }

    override public function clone():Event
    {
        return new SizeEvent(type, params );
    }
}
}

So when you add your items to the gallery you write the following:

  galleryItem.addEventListener(SizeEvent.CHANGE , sizeEventListener );

And when an animation ends in the galleryItem

 private function onAnimationComplete():void
 {
    var params:Object = {x: this.x , y:this.y , width:this.width , 
                          height:this.height, //any info you need  etc... };

    dispatchEvent( new SizeEvent( SizeEvent.CHANGE , params ));
 }
PatrickS
+1 for the first solution. @mik if there was no animation then you could extend the child (assuming it is a MovieClip) and override the setter properties for width and height so that a similar dispatchEvent could be done.
Allan
@Allan Thanks! I never use the second solution actually... but i think it's good to know about both approaches
PatrickS
Thanks for the answer. I knew the overriding approach but it's not the solution since the gallery items are unknown. Same for Patrick's.. I don't when the animation starts or begins since it has nothing to do with the gallery, it's the item's own animation.Another solution is maybe forcing a ItemGalleryDispatcher interface which should be implemented by every gallery item, and need to implement a "addItemListener" which will be the gallery itself, and the responsibility to report a change of size will be the gallery's item itself. but thats complicates things. THANKS FOR THE ANSWERS
mik