PDA

View Full Version : Custom Event for height change AS3 (Flash)



samroni
February 18th, 2010, 06:02 AM
Hey Guys.... how can i add an event listener to detect when the height of a DisplayObject changes.


Obviously, Event.RESIZE doesn't work.


should i make a new custom event? i don't know how to slip the "height change" in the custom event..

IQAndreas
February 18th, 2010, 06:16 AM
Well, if you are using classes, the easiest thing do to would be to override the "height" (and while you are at it, maybe even width) properties. You don't need to create an entirely new Event class, just the "string data" that tells the name of the event. You can also use the RESIZE Event if you want.


public class MyClass extends MovieClip
{
public function MyClass()
{
//...
}


//Override the height and width properties
public override function set height(value:Number)
{
//Make sure the height is set FIRST
super.height = value;

//THEN, create the new event
var event:Event = new Event(Event.RESIZE);

//And now dispatch the created event
this.dispatchEvent(event);
}

//Do the same for width

}

Do you want more information on Events, just ask. :) (Though, try Google first, but I'm here if you get stuck)

samroni
February 18th, 2010, 07:07 AM
Well, if you are using classes, the easiest thing do to would be to override the "height" (and while you are at it, maybe even width) properties. You don't need to create an entirely new Event class, just the "string data" that tells the name of the event. You can also use the RESIZE Event if you want.

ActionScript Code:

public class MyClass extends MovieClip
{
public function MyClass()
{
//...
}


//Override the height and width properties
public override function set height(value:Number)
{
//Make sure the height is set FIRST
super.height = value;

//THEN, create the new event
var event:Event = new Event(Event.RESIZE);

//And now dispatch the created event
this.dispatchEvent(event);
}

//Do the same for width

}




Do you want more information on Events, just ask. :) (Though, try Google first, but I'm here if you get stuck)

ooo yeah, great... i got this... HOWEVER... i'm kinda in situation where i can't use override function. i'm using static class. any other way?

IQAndreas
February 18th, 2010, 07:55 AM
Please post your code. If you are using a static class, you already know when the height is changed, right?

Perhaps you are looking for "properties" or "getter/setters". They are treated exactly like variables, but are really functions in disguise. In my example, I overrode one of them, and it's the exact same syntax, except without the "override" command.

Or do you mean you are not writing the code for the class, but want to detect when a DIFFERENT class changed height?