📄 sequencebase.as
字号:
/** * Copyright (c) 2007 Moses Gunesch * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */package org.goasap.utils { import flash.events.Event; import flash.utils.getQualifiedClassName; import org.goasap.PlayableBase; import org.goasap.errors.InstanceNotAllowedError; import org.goasap.events.GoEvent; import org.goasap.events.SequenceEvent; import org.goasap.interfaces.IPlayable; import org.goasap.managers.Repeater; /** * Dispatched when the sequence starts. * @eventType org.goasap.events.START */ [Event(name="START", type="org.goasap.events.GoEvent")] /** * Dispatched when the sequence advances to its next step. * @eventType org.goasap.events.SequenceEvent.ADVANCE */ [Event(name="ADVANCE", type="org.goasap.events.SequenceEvent")] /** * Dispatched when the sequence is paused successfully. * @eventType org.goasap.events.PAUSE */ [Event(name="PAUSE", type="org.goasap.events.GoEvent")] /** * Dispatched when the sequence is resumed successfully. * @eventType org.goasap.events.RESUME */ [Event(name="RESUME", type="org.goasap.events.GoEvent")] /** * Dispatched at the end the group if <code>repeater.cycles</code> is set to * a value other than one, just before the sequence starts its next play cycle. * @eventType org.goasap.events.CYCLE */ [Event(name="CYCLE", type="org.goasap.events.GoEvent")] /** * Dispatched when the sequence is manually stopped, which may also occur * if one of its step instances is manually stopped outside the sequence. * @eventType org.goasap.events.STOP */ [Event(name="STOP", type="org.goasap.events.GoEvent")] /** * Dispatched when the sequence successfully finishes. (In SequenceCA this event * is not fired until all custom-advanced steps have dispatched STOP or COMPLETE.) * @eventType org.goasap.events.COMPLETE */ [Event(name="COMPLETE", type="org.goasap.events.GoEvent")] /** * This base class should not be used directly, use it to build sequencing classes. * * <p>When subclassing, follow the instructions in the comments of the protected * methods to add a standard set of public getters and methods that work with the * specific datatype of your SequenceStep subclass, if you create one. (This system * is designed to work around the restrictiveness of overrides in AS3 which don't * allow you to redefine datatypes.) See Sequence and SequenceCA for examples.</p> * * @see Sequence * @see SequenceCA * * @author Moses Gunesch */ public class SequenceBase extends PlayableBase implements IPlayable { // -== Public Properties ==- /** * The number of steps in the sequence. */ public function get length(): int { return (_steps ? _steps.length : 0); } /** * The current play index of the sequence, starting a 0. */ public function get playIndex(): int { return _index; } /** * Get or set the list of SequenceStep instances that defines the sequence. * * <p> * When setting this property, each item must implement IPlayable that uses * PlayableBase play-state constants and dispatches STOP or COMPLETE when finished. * Each item is automatically wrapped in a SequenceStep if it is of any other IPlayable * type, such as a GoItem or PlayableGroup. Setting this property stops any sequence * play currently in progress. * </p> * @see #_getStepAt() * @see #_getStepByID() * @see #_getCurrentStep() * @see #_getLastStep() */ public function get steps():Array { return _steps; } public function set steps(a:Array):void { if (_state!=STOPPED) stop(); while (_steps.length > 0) _removeStepAt(_steps.length-1); for each (var item:Object in a) if (item is IPlayable) _addStep(item as IPlayable); } /** * The sequence's Repeater instance, which may be used to make * the sequence loop and play more than one time. * * <p>The Repeater's cycles property can be set to an integer, or * to Repeater.INFINITE or 0 to repeat indefinitely.</p> * * <pre>var seq:Sequence = new Sequence(tween1, tween2, tween3); * seq.repeater.cycles = 2; * seq.start(); * trace(seq.repeater.currentCycle); // output: 0 * * seq.skipTo(4); // moves to 2nd action in 2nd cycle * trace(seq.repeater.currentCycle); // output: 1</pre> * * <p>(The repeater property replaces the repeatCount and currentCount * parameters in earlier releases of SequenceBase).</p> */ public function get repeater(): Repeater { return _repeater; } // -== Protected Properties ==- /** @private */ protected var _index: int = 0; /** @private */ protected var _steps: Array; /** @private */ protected var _repeater: Repeater; // -== Public Methods ==- /** * Constructor. * * @param items Any number of IPlayable instances (e.g. LinearGo, PlayableGroup, * SequenceStep) as separate arguments, or a single array of them. */ public function SequenceBase(...items) { super(); var className:String = getQualifiedClassName(this); if (className.slice(className.lastIndexOf("::")+2) == "SequenceBase") { throw new InstanceNotAllowedError("SequenceBase"); } _steps = new Array(); if (items.length > 0) { steps = ((items[ 0 ] is Array) ? items[ 0 ] : items); } _repeater = new Repeater(); _repeater.setParent(this); } // -== IPlayable implementation ==- /** * Begins a sequence. * * <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 unless there are no steps in the sequence. */ public function start() : Boolean { if (_steps.length==0) return false; stop(); _state = PLAYING; _getCurrentStep().start(); dispatchEvent(new GoEvent( GoEvent.START )); _playRetainer[ this ] = 1; // Developers - Important! Look up _playRetainer. return true; } /** * Stops all activity and dispatches a GoEvent.STOP event. * * @return Returns true unless sequence was already stopped. */ public function stop() : Boolean { if (_state==STOPPED || _steps.length==0) return false; _state = STOPPED; var stepState:String = _getCurrentStep().state; // TODO: this won't see the _trailingSteps state in SequenceCA _getCurrentStep().stop(); if (_steps.length-_index > 1 || stepState!=STOPPED) dispatchEvent(new GoEvent( GoEvent.STOP )); else dispatchEvent(new GoEvent( GoEvent.COMPLETE )); _index = 0; _repeater.reset(); delete _playRetainer[ this ]; // Developers - Important! Look up _playRetainer. return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -