about
history
syntax
visu
arcade
goodies
wrappers
efficiency
examples
experiments
tabageos
tutorials
actiontad logo white
Global Events

Global events are what I call events that can effect a group of objects at once.
The idea is that one dispatch of an event will cause many different functions to fire.
With each function that gets called having direct reference to the object it handles.
Either in the event, or by being part of the object itself.

The Result


See the example in its own window here.


globalEventsExample.as

package Tutorials
{

	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	
	public class globalEventsExample extends Sprite 
	{
		
		private var customEvent:Event;
		
		/**
		 *  This class is an example of using a globally accessible EventDispatcher to easily control a group of objects via one Event.
		 * 
		 *  In this example we will use the stage as the global EventDispatcher, 
		 *  however a public static EventDispatcher could be used, or any EventDispatcher
		 *  that all objects in the group can reference.
		 *  
		 *  The basic idea is that by having a central dispatched Event to which all or a group of objects can tap into,
		 *  you can quickly remove/add back all the objects invovled by altering the centrally dispatched Event, 
		 *  instead of each individual objects Event listener.
		 * 
		 * 
		 */
		public function globalEventsExample() {
			customEvent = new Event("CustomEvent");
			if(stage) ini(null) else addEventListener(Event.ADDED_TO_STAGE, ini, false, 0, true);
		}
		
		private function ini(e:Event):void {
			
			removeEventListener(Event.ADDED_TO_STAGE, ini);
			var t:TextField = new TextField(); t.width = 300;
			t.text = "click anywhere to stop the movement";
			addChild(t);
			for (var i:int = 0; i < 25; i++) {
				var shape:Shape = new Shape();
				with (shape.graphics) {
					beginFill(Math.random() * 0xFFFFFF);
					drawCircle(0, 0, 15);
				}
				this.addChild(shape);
				shape.stage.addEventListener("CustomEvent", individualFunction(shape));
				
				//Note: I've used the individualFunction method for the sake of keeping this all in one class.
				//In a real situation, shapes would be custom classes with their own individual methods. 
				//(so for example, myClass.someMethod, with someMethod being an event handler)
				//In the case of LoopEventSubscribers, they each have a public loopEventHandler method that takes a LoopEvent.
			}
			
			//shape.stage and this.stage are the same thing, there is only 1 stage.
			//Note: If you like, you can use some other EventDispatcher instead. 
			//With multiple classes, however, such a dispatcher would need to be passed around, or referenced in each class somehow.
			
			this.stage.addEventListener(Event.ENTER_FRAME, dispatchCustomEvent);
			
			//Because we have used a globally accessible EventDispatcher (the stage) for the loop functions, 
			//we do not need to remove each loop from each shape.
			//Instead we only need to stop the CustomEvent from being dispatched on the stage, doing so will stop all loops.
			
			this.stage.addEventListener(MouseEvent.CLICK, toggleAllLoops);
		}
		
		private function toggleAllLoops(e:Event):void {
			if (stage && stage.hasEventListener(Event.ENTER_FRAME)) stage.removeEventListener(Event.ENTER_FRAME, dispatchCustomEvent)
			else if (stage) stage.addEventListener(Event.ENTER_FRAME, dispatchCustomEvent);
		}
		
		private function dispatchCustomEvent(e:Event):void {
			stage.dispatchEvent(customEvent);
			//In LoopEventSubscribers, this idea is taken one step further, 
			//and the dispatch only happens based on calculations
			//for each objects pace with the main loop. 
			
			//In this case the loop is EnterFrame; in LoopEventSubscribers, LoopEvent.
			//see the com.actiontad.basicGameObjects.LoopEventSubscriber class for more info.
		}
		
		private function individualFunction(forThis:Shape):Function {
			var loopFunction:Function = function(e:Event):void {
				var ranX:Number = Math.random() * stage.stageWidth;
				var ranY:Number = Math.random() * stage.stageHeight;
				forThis.x = ranX;
				forThis.y = ranY;
				
			}; return loopFunction;
		}
		
	}
}











actiontad twitter.com/actiontad terms