AS3 Passing data between objects/classes
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 th开发者_C百科is 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.
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:
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.
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);
}
}
}
精彩评论