📄 playablegroup.as
字号:
else { secondary.start(); } } break; } } } // Saved until after possible play-state changes. Now we can base listening on this group's state. _children[ item ] = false; if (_state!=STOPPED) listenTo(item); return true; } /** * Removes a single IPlayable from the children array. * * <p>Note that if play is in progress when a child is added it does not * interrupt play and the child is monitored for completion along with * others.</p> * * @param item Any instance that implements IPlayable and uses PlayableBase's play-state constants. * @return Success. */ public function removeChild(item:IPlayable): Boolean { var v:* = _children[ item ]; if (v===null) return false; if (v===true) unListenTo( item ); delete _children[ item ]; return true; } /** * Test whether any child has a particular play state, based on * the int constants in the PlayableBase class. * * <pre> * // Example: resume a paused group * if ( myGroup.anyChildHasState(PlayableBase.PAUSED) ) { * myGroup.resume(); * } * </pre> */ public function anyChildHasState(state:String): Boolean { for (var item:Object in _children) if ((item as IPlayable).state==state) return true; return false; } // -== IPlayable implementation ==- /** * Calls start on all children. * * <p>If the group is active when this method is called, a <code>stop</code> call * is automated which will result in a GoEVent.STOP event being dispatched.</p> * * @return Returns true if any child in the group starts successfully. */ public function start() : Boolean { stop(); var r:Boolean = false; for (var item:Object in _children) { var started:Boolean = (item as IPlayable).start(); if (started) listenTo(item as IPlayable); r = (started || r); } if (!r) return false; // all starts failed _state = PLAYING; dispatchEvent(new GoEvent( GoEvent.START)); _playRetainer[ this ] = 1; // Developers - Important! Look up _playRetainer. return true; } /** * If the group is active, this method stops all child items and * dispatches a GoEvent.STOP event. * * @return Returns true only if all children in the group stop successfully. */ public function stop() : Boolean { if (_state == STOPPED) return false; _state = STOPPED; _repeater.reset(); delete _playRetainer[ this ]; // Developers - Important! Look up _playRetainer. if (_listeners==0) { dispatchEvent(new GoEvent( GoEvent.COMPLETE )); return true; } var r:Boolean = true; for (var item:Object in _children) { unListenTo(item as IPlayable); r = ((item as IPlayable).stop() && r); } dispatchEvent(new GoEvent( GoEvent.STOP )); return r; } /** * Calls <code>pause</code> on all children. * * @return Returns true only if all playing children in the group paused successfully * and at least one child was paused. */ public function pause() : Boolean { if (_state!= PLAYING) return false; var r:Boolean = true; var n:uint = 0; for (var item:Object in _children) { var success:Boolean = (item as IPlayable).pause(); if (success) n++; r = (r && success); } if (n>0) { _state = PAUSED; // state should reflect that at least one item was paused, // while return value may indicate that not all pause calls succeeded. dispatchEvent(new GoEvent( GoEvent.PAUSE )); } return (n>0 && r); } /** * Calls <code>resume</code> on all children. * * @return Returns true only if all paused children in the group resumed successfully * and at least one child was resumed. */ public function resume() : Boolean { if (_state!= PAUSED) return false; var r:Boolean = true; var n:uint = 0; for (var item:Object in _children) { var success:Boolean = (item as IPlayable).resume(); if (success) n++; r = (r && success); } if (n>0) { _state = PLAYING; // state should reflect that at least one item was resumed, // while return value may indicate that not all resume calls succeeded. dispatchEvent(new GoEvent( GoEvent.RESUME )); } return (n>0 && r); } /** * Calls <code>skipTo</code> on all children. * * @return Returns true only if all children in the group skipTo the position successfully * and at least one child was affected. */ public function skipTo(position : Number) : Boolean { var r:Boolean = true; var n:uint = 0; position = _repeater.skipTo(_repeater.cycles, position); // TODO: TEST for (var item:Object in _children) { r = ((item as IPlayable).skipTo(position) && r); listenTo(item as IPlayable); n++; } _state = (r ? PLAYING : STOPPED); return (n>0 && r); } // -== Protected Methods ==- /** * @private * Internal handler for item completion. * @param event GoEvent dispatched by child item. */ protected function onItemEnd(event:GoEvent) : void { unListenTo(event.target as IPlayable); if (_listeners==0) { complete(); } } /** * @private * Internal handler for group completion. */ protected function complete() : void { if (_repeater.next()) { dispatchEvent(new GoEvent( GoEvent.CYCLE )); for (var item:Object in _children) { var started:Boolean = (item as IPlayable).start(); if (started) listenTo(item as IPlayable); } } else { stop(); } } /** * @private * Internal. Listen for item completion, keeping tight track of listeners. * @param item Any instance that extends IPlayable (IPlayable itself should not be used directly). */ protected function listenTo(item:IPlayable) : void { if (_children[ item ] === false) { item.addEventListener(GoEvent.STOP, onItemEnd, false, 0, true); item.addEventListener(GoEvent.COMPLETE, onItemEnd, false, 0, true); _children[ item ] = true; _listeners++; } } /** * @private * Internal. Stop listening for item completion. * @param item Any instance that extends IPlayable (IPlayable itself should not be used directly). * @return Number of completion listeners remaining. */ protected function unListenTo(item:IPlayable) : void { if (_children[ item ] === true) { item.removeEventListener(GoEvent.STOP, onItemEnd); item.removeEventListener(GoEvent.COMPLETE, onItemEnd); _children[ item ] = false; _listeners--; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -