如何在ActionScript传递变量? [英] How to pass variables in ActionScript?

查看:89
本文介绍了如何在ActionScript传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一个页面变量传递给在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 ) );
   }

这篇关于如何在ActionScript传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆