package org.as3lib.kitchensync.action { import flash.utils.getQualifiedClassName; import org.as3lib.kitchensync.core.*; /** * A group of actions that executes all at once the group is started and the delay is reached. */ public class lib_flex_animation_code_10_org_as3lib_kitchensync_action_KSParallelGroup extends AbstractActionGroup { protected var _runningChildren:int = 0; public function get childrenAreRunning():Boolean { return _runningChildren > 0; } /** * Constructor. * * @throws TypeError - if any children are not of type AbstractSynchronizedAction. * * @params children - a list of AbstractSynchronizedActions that will be added as children of the group. */ public function lib_flex_animation_code_10_org_as3lib_kitchensync_action_KSParallelGroup (... children) { super(); for (var i:int = 0; i < children.length; i++) { if (children[i] is IAction) { var action:IAction = IAction(children[i]); addAction(action); } else { throw new TypeError ("All children must be of type IAction. Make sure you are not calling start() on the objects you've added to the group. Found " + getQualifiedClassName(children[i]) + " where IAction was expected."); } } } /** * When the first update occurs, all of the child actions are started simultaniously. */ override public function update(currentTimestamp:Timestamp):void { if (startTimeHasElapsed && !childrenAreRunning) { // reset the number of running children. _runningChildren = 0; // for all child actions for (var i:int=0; i < _childActions.length; i++) { var childAction:IAction = IAction(_childActions[i]); // add a listener to each action so that the completion of the entire group can be tracked. childAction.addEventListener(KitchenSyncEvent.START, onChildStart); childAction.addEventListener(KitchenSyncEvent.COMPLETE, onChildFinished); // start the child action childAction.start(); // add one running child. _runningChildren++; } // once this has started, it doesn't need updates anymore. unregister(); } } /** * Called when child actions are completed. After each is finished, checks to see if the entire set is * complete. If not, it waits for the next child. * Completed children are removed from the array so they can be garbage collected. * * @param event - The SynchronizerEvent.COMPLETE * @param event - The SynchronizerEvent.CHILD_COMPLETE */ override protected function onChildFinished (event:KitchenSyncEvent):void { super.onChildFinished(event); var childAction:IAction = IAction(event.target); childAction.removeEventListener(KitchenSyncEvent.COMPLETE, onChildFinished); childAction.removeEventListener(KitchenSyncEvent.START, onChildStart); _runningChildren--; if (_runningChildren == 0) { complete(); } } override public function stop():void { super.stop(); _runningChildren = 0; } override public function clone():IAction { var clone:lib_flex_animation_code_10_org_as3lib_kitchensync_action_KSParallelGroup = new lib_flex_animation_code_10_org_as3lib_kitchensync_action_KSParallelGroup(); for (var i:int = 0; i < _childActions.length; i++) { var action:IAction = getChildAtIndex(i).clone(); clone.addActionAtIndex(action, i); } clone.delay = _delay; clone.autoDelete = _autoDelete; return clone; } override public function toString():String { return "Parallel group containing " + _childActions.length + " children"; } }}