对象/类之间AS3传递数据对象、数据

2023-09-08 14:02:11 作者:正宗二货·仿冒必究

所以IA构建不同食物的分类菜单。我有一类为类别(按钮),基本上将返回一个字符串沙拉,饮料等。我现在有另一个阶级的菜单项的类别内的项目,而这种处理尺寸,如小, MED,大,等等。现在我的问题是,当我返回沙拉,我想调用其中包含沙拉的所有元素的数组,其发送到菜单项,将填充菜单。到目前为止,我有类对象和菜单对象设置两个。我只是不能似乎能够发送的类别对象返回数据,并把它传递给菜单对象。这两者都加入到阶段,如下所示:

So i a building a categorized menu of different foods. I have a class for "categories" (buttons) which essentially will return a string "salads", "drinks", etc. I now have another class "menuItems" for items within categories and this handles sizes such as "small", "med", "large", etc. My problem now is that when i return "salads", i want to invoke an array which contains all the elements of salads, send it to menuItems which will populate the menu. So far i have both the category objects and the menu object setup. I just cant seem to be able to send the data that the category object is returning and pass it to the menu object. Both of which are added to the stage as shown below:

如果有,我可以说,加入所有这些类一类,以便他们可以互相交谈,这将是巨大的,但我不知道如何做到这一点的方式。

If there was a way that i could say add all these classes to one class so that they can talk to each other that would be great but i dont know how to do this.

一直停留了几个小时,请任何帮助是极大的AP preciated。

Been stuck for hours, please any help is greatly appreciated.

推荐答案

之一对象之间传递数据的常用方法是分派的定制事件。这具有减少类之间的依赖性的优点。比方说,例如,你想添加一个饮料类的结构,如果所有的类都是相互关联的彼此,它可能难以更新应用程序。而另一方面,随着事件,您可以决定创建一个新的自定义事件或饮料属性添加到您当前的事件,打破东西的机会是有限的。

One of the common way of passing data between objects is to dispatch custom events. This has the advantage of reducing dependency between classes. Let's say , for instance , that you wanted to add a "Drinks" class to your structure, if all classes are interconnected with one another , it may prove difficult to update your application. On the other hand, with events , you can either decide to create a new CustomEvent or add a drinks property to your current event , the chances of something breaking are more limited.

在实践中,您将需要一个事件调度器会发送和接收事件,然后通知相关的对象。

Practically, you will need an event dispatcher that will send and receive events and then inform the relevant object.

下面是一个广泛的例子:

Here's a broad example:

取每个包装并保存在自己的文件,使用。作为扩展名(例如:Main.as)赋予其类的名称。所有文件必须驻留在同一个文件夹中。 Main.as将是你的切入点,如果你使用的是Flash CS,这将是你的文档类。

Take each package and save them in their own file, giving it the name of class with the .as extension ( ex: Main.as ). All files should reside in the same folder. Main.as will be your entry point, if you're using Flash CS , this will be your Document Class.

花点时间尽管查看AS3一些教程,这是一个建议,我常给,因为这将节省您的麻烦和时间浪费试图解决基本问题的负载。尝试科林·穆克的失落的Actionscript周末视频教程,比如,你应该找到它的Adobe电视。

Take the time to check some tutorials about AS3 though , this is an advice I often give as it will save you loads of headaches and time wasted trying to solve basic issues. Try Colin Mook's Lost Actionscript Weekend Video Tutorial, for instance, you should find it on Adobe TV.

 package
 { 
 import flash.display.Sprite;
     import flash.events.EventDispatcher;

     public class Main extends Sprite
     {
      private var dispatcher:EventDispatcher; 

      private var salads:Categories; 
      private var menu:MenuItems; 

      public function Main()
      {
          dispatcher = new EventDispatcher();
          menu = new MenuItems( dispatcher );
          salads = new Categories( dispatcher );
      }
    }
 }


                         ******
 package
 {
   import flash.events.Event;

   public class MenuEvent extends Event
   {
     //Your Custom Event
     public static const CATEGORY:String = "Category";

     //Here I type the data property as an Object, but it could also 
     //be a String , depending on the type of info you need to pass
     public var data:Object;

     public function MenuEvent( type:String , data:Object ):void
     {
       super ( type );
       this.data = data;
     }

     override public function clone():MenuEvent
     {
         return new MenuEvent( type , data );
     }

 }
                         ******
 package 
 {

 import flash.events.EventDispatcher;
 import flash.events.MouseEvent;

     public class Categories
     {

       private var dispatcher:EventDispatcher;

       public function Categories(dispatcher:EventDispatcher ):void
       {
         this.dispatcher = dispatcher;

         //for this example, i dispatch the event here
         //so you don't have to create a button...
         dispatcher.dispatchEvent( new MenuEvent( MenuEvent.CATEGORY , "salads"  ) );
       }

       private function clickHandler( event:MouseEvent ):void
       {
         //for instance , in the "salads" category...
         dispatcher.dispatchEvent( new MenuEvent( MenuEvent.CATEGORY , "salads"  ) );
       }
     }
  }

                         ******
  package 
  {

 import flash.events.EventDispatcher;

     public class MenuItems
     {            
       private var dispatcher:EventDispatcher;

       public function MenuItems(dispatcher:EventDispatcher ):void
       {
         this.dispatcher = dispatcher;
         dispatcher.addEventListener( MenuEvent.CATEGORY , menuEventHandler );
       }

       private function menuEventHandler( event:MenuEvent ):void
       {
          trace( event.data as String);
       }
      }
    }