How to pass variables in ActionScript?

前端 未结 2 1849
长情又很酷
长情又很酷 2021-01-26 15:43

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

2条回答
  •  感动是毒
    2021-01-26 16:13

    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 ) );
       }
    

提交回复
热议问题