如何在ActionScript传递变量?变量、如何在、ActionScript

2023-09-08 13:10:54 作者:养只恶鬼

如何从一个页面变量传递给在ActionScript另一页? 我得到了一些解决方案,这可以通过使用CustomEvents在ActionScript中完成,但我无法找到可以理解的,简单的解决方案。任何人都可以解释如何用一个小例子传递变量?请帮助?

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 understandable and easy solution. Can anyone explain how to pass variables with a small example ? Please Help ?

推荐答案

最好的做法是使用事件,以确保您的类是不是太紧密耦合。下面是做这件事,但当然有很多不同的方法可用....

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