How to pass variables from one page to another page in ActionScript ? I got some solution that this can be done using CustomEvents in ActionScript, but i couldn\'t found underst
Best practice is to use events so as to ensure that your Classes are not too tightly coupled. Here's one way to do it but there are of course many different approaches available....
//In your main class
private var dispatcher:EventDispatcher = new EventDispatcher();
private var page1:A;
private var page2:B;
public function Main()
{
page1 = new A( dispatcher );
page2 = new B( dispatcher );
}
//In Class A ( or Class B )
private var dispatcher:EventDispatcher;
public function A( dispatcher:EventDispatcher )
{
this.dispatcher = dispatcher;
dispatcher.addEventListener( CustomEvent.EXAMPLE , customEventListener );
}
private function customEventListener( event:CustomEvent ):void
{
trace( event.type , event.data );
}
private function anyMethod(data:Object):void
{
//using a CustomEvent with a data property
//also passing a type can help in selecting between actions
dispatcher.dispatchEvent( new CustomEvent( CustomEvent.EXAMPLE , data ) );
}