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
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 ) );
}
This question has more or less been answered by PatrickS, but the explaination of creating custom events was left out. So to more or less add on to PatrickS’s answer, the following is an example of a custom event.
package com.events
{
import flash.events.Event;
public class CustomEvent extends Event
{
public static const EXAMPLE:String = "example";
public var data:String;
public function CustomEvent(p_type:String,
p_data:String,
p_bubbles:Boolean = false,
p_cancelable:Boolean = false):void
{
super(p_type, p_bubbles, p_cancelable);
data = p_data;
}// end function
override public function clone():Event
{
return new CustomEvent(type, data, bubbles, cancelable);
}// end function
}// end class
}// end package
To create a custom event you have to extend Event or a subclass of it, e.g. MouseEvent. The CustomEvent class is a custom event that extends Event.
When creating a custom event you can create custom event types. Event types are string values that are given as arguments when dispatching an event. When parsed, it is highly recommended to give them in the form of a constant to make them typesafe. It is for that purpose that you create a public static constant property for an Event. For example, the event Event has the event type COMPLETE which is a public static constant which has a string value “complete”. In this case, the CustomEvent has an event type EXAMPLE with a string value of “example”.
When creating a custom event you can create event properties that store values that are given when the event is dispatched. For example, the event ProgressEvent has the event property bytesLoaded. In this case the CustomEvent has a property called data.
When creating a custom event, the constructor has to have a parameter for the event type, bubbles flag and cancellable state. This is where you can also add parameters for values you want to be stored in the event’s properties and be dispatched with it. In this case, there is a parameter called p_data which is a string type. You then use the super keyword to invoke the constructor of the parent class Event and parse the arguments given when dispatching the custom event to the parent class’s constructor. I won’t go into detail about this because that goes into class inheritance and you really don’t need to know that. Next you assign the event properties’ values with the data that is parsed when the event is dispatched. In this case the property data of the event CustomEvent is assigned the value from the p_data argument parsed via CustomEvent’s constructor.
I hope this helped :)