⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tweengroup.as

📁 一个地图flash应用
💻 AS
📖 第 1 页 / 共 3 页
字号:
		 * 
		 * @param $forcePlay Forces the group to resume() if it hasn't completed yet or restart() if it has.
		 */
		public function reverse($forcePlay:Boolean=true):void {
			_reversed = !_reversed;
			var i:int, tween:TweenLite, proxy:ReverseProxy, endTime:Number, startTime:Number, initTime:Number, prog:Number, timeScale:Number, timeOffset:Number = 0, isFinished:Boolean = false;
			var time:Number = (!isNaN(_pauseTime)) ? _pauseTime : TweenLite.currentTime;
			if (this.endTime <= time) {
				timeOffset = int(this.endTime - time) + 1;		
				isFinished = true;
			}
			for (i = _tweens.length - 1; i > -1; i--) {
				tween = _tweens[i];
				
				if (tween is _TweenMax) { //TweenMax instances already have a "reverseEase()" function. I don't us "if (tween is TweenMax)" because it would bloat the file size by having to import TweenMax, so developers can just use this class with TweenLite to keep file size to a minimum if they so choose.
					startTime = tween.startTime;
					initTime = tween.initTime;
					(tween as Object).reverse(false, false);
					tween.startTime = startTime;
					tween.initTime = initTime;
				} else if (tween.ease != tween.vars.ease) {
					tween.ease = tween.vars.ease;
				} else {
					proxy = new ReverseProxy(tween);
					tween.ease = proxy.reverseEase;
				}
				
				timeScale = tween.combinedTimeScale;
				prog = (((time - tween.initTime) / 1000) - tween.delay / timeScale) / tween.duration * timeScale;
				startTime = int(time - ((1 - prog) * tween.duration * 1000 / timeScale) + timeOffset);
				tween.initTime = int(startTime - (tween.delay * (1000 / timeScale)));
				
				if (tween.startTime != 999999999999999) {
					tween.startTime = startTime;
				}
				if (tween.startTime > time) { //don't allow tweens with delays that haven't expired yet to be active
					tween.enabled = true;
					tween.active = false;
				}
				
			}
			updateTimeSpan();
			if ($forcePlay) {
				if (isFinished) {
					setProgress(0, true);
				}
				resume();
			}
		}
		
		/**
		 * Provides an easy way to determine which tweens (if any) are currently active. Active tweens are not paused and are in the process of tweening values.
		 * 
		 * @return An Array of TweenLite/TweenFilterLite/TweenMax instances from the group that are currently active (in the process of tweening)
		 */
		public function getActive():Array {
			var a:Array = [];
			if (isNaN(_pauseTime)) {
				var i:int, time:Number = TweenLite.currentTime;
				for (i = _tweens.length - 1; i > -1; i--) {
					if (_tweens[i].startTime <= time && getEndTime(_tweens[i]) >= time) {
						a[a.length] = _tweens[i];
					}				
				}
			}
			return a;
		}
		
		/**
		 * Merges (combines) two TweenGroups. You can even control the index at which the tweens are spliced in, or if you don't define one, the tweens will be added to the end.
		 * 
		 * @param $group The TweenGroup to add
		 * @param $startIndex The index at which to start splicing the tweens from the new TweenGroup. For example, if tweenGroupA has 3 elements, and you want to add tweenGroupB's tweens right after the first one, you'd call tweenGroupA.mergeGroup(tweenGroupB, 1);
		 */
		public function mergeGroup($group:TweenGroup, $startIndex:Number=NaN):void {
			if (isNaN($startIndex) || $startIndex > _tweens.length) {
				$startIndex = _tweens.length;
			}
			var tweens:Array = $group.tweens;
			var l:uint = tweens.length, i:int;
			for (i = 0; i < l; i++) {
				_tweens.splice($startIndex + i, 0, tweens[i]);
			}
			realign();
		}
		
		/**
		 * Removes all tweens from the group and kills the tweens using TweenLite.removeTween()
		 * 
		 * @param $killTweens Determines whether or not all of the tweens are killed (as opposed to simply being removed from this group but continuing to remain in the rendering queue)
		 */
		public function clear($killTweens:Boolean=true):void {
			for (var i:int = _tweens.length - 1; i > -1; i--) {
				if ($killTweens) {
					TweenLite.removeTween(_tweens[i], true);
				}
				_tweens[i] = null;
				_tweens.splice(i, 1);
			}
		}
		
		/** 
		 * Analyzes all of the tweens in the group and determines the overall init, start, and end times as well as the overall duration which 
		 * are necessary for accurate management. Normally a TweenGroup handles this internally, but if tweens are manipulated independently
		 * of TweenGroup or if a tween has its "loop" or "yoyo" special property set to true, it can cause these variables to become uncalibrated
		 * in which case you can use updateTimeSpan() to recalibrate. 
		 */
		public function updateTimeSpan():void {
			if (_tweens.length == 0) {
				this.endTime = _startTime = _initTime = 0;
			} else {
				var i:int, start:Number, init:Number, end:Number, tween:TweenLite;
				tween = _tweens[0];
				_initTime = tween.initTime;
				
				_startTime = _initTime + (tween.delay * (1000 / tween.combinedTimeScale));
				this.endTime = _startTime + (tween.duration * (1000 / tween.combinedTimeScale));
				
				for (i = _tweens.length - 1; i > 0; i--) {
					tween = _tweens[i];
					init = tween.initTime;
					
					start = init + (tween.delay * (1000 / tween.combinedTimeScale));
					end = start + (tween.duration * (1000 / tween.combinedTimeScale));
					
					if (init < _initTime) {
						_initTime = init;
					}
					if (start < _startTime) {
						_startTime = start;
					}
					if (end > this.endTime) {
						this.endTime = end;
					}
				}
				
				if (this.expired && this.endTime > TweenLite.currentTime) {
					this.expired = false;
					_unexpired[_unexpired.length] = this;
				}
				
			}
		}
		

//---- STATIC PUBLIC FUNCTIONS -----------------------------------------------------------------------------------

		/**
		 * Parses an Array that contains either TweenLite/TweenFilterLite/TweenMax instances or Objects that are meant to define tween instances.
		 * Specifically, they must contain at LEAST "target" and "time" properties. For example: TweenGroup.parse([{target:mc1, time:2, x:300},{target:mc2, time:1, y:400}]);
		 *  
		 * @param $tweens An Array of either TweenLite/TweenFilterLite/TweenMax instances or Objects that are meant to define tween instances. For example [{target:mc1, time:2, x:300},{target:mc2, time:1, y:400}]
		 * @param $BaseTweenClass Defines which tween class should be used when parsing objects that are not already TweenLite/TweenFilterLite/TweenMax instances. Choices are TweenLite, TweenFilterLite, or TweenMax.
		 * @return An Array with only TweenLite/TweenFilterLite/TweenMax instances
		 */
		public static function parse($tweens:Array, $DefaultTweenClass:Class=null):Array {
			if ($DefaultTweenClass == null) {
				$DefaultTweenClass = TweenLite;
			}
			var a:Array = [], i:int, target:Object, duration:Number;
			for (i = 0; i < $tweens.length; i++) {
				if ($tweens[i] is TweenLite) {
					a[a.length] = $tweens[i];
				} else {
					target = $tweens[i].target;
					duration = $tweens[i].time;
					delete $tweens[i].target;
					delete $tweens[i].time;
					a[a.length] = new $DefaultTweenClass(target, duration, $tweens[i]);
				}
			}
			return a;
		}
		
		
		/**
		 * Provides an easy way to tween multiple objects to the same values. It also accepts a few special properties, like "stagger" which 
		 * staggers the start time of each tween. For example, you might want to have 5 MovieClips move down 100 pixels while fading out, and stagger 
		 * the start times slightly by 0.2 seconds, you could do:  
		 * TweenGroup.allTo([mc1, mc2, mc3, mc4, mc5], 1, {y:"100", alpha:0, stagger:0.2});
		 * 
		 * @param $targets An Array of objects to tween.
		 * @param $duration Duration (in seconds) of the tween
		 * @param $vars An object containing the end values of all the properties you'd like to have tweened (or if you're using the TweenGroup.allFrom() method, these variables would define the BEGINNING values). Additional special properties: "stagger", "onCompleteAll", and "onCompleteAllParams"
		 * @param $DefaultTweenClass Defines which tween class to use. Choices are TweenLite, TweenFilterLite, or TweenMax.
		 * @return TweenGroup instance
		 */
		public static function allTo($targets:Array, $duration:Number, $vars:Object, $DefaultTweenClass:Class=null):TweenGroup {
			if ($DefaultTweenClass == null) {
				$DefaultTweenClass = TweenLite;
			}
			var i:int, target:Object, vars:Object, p:String;
			var group:TweenGroup = new TweenGroup(null, $DefaultTweenClass, ALIGN_INIT, $vars.stagger || 0);
			group.onComplete = $vars.onCompleteAll;
			group.onCompleteParams = $vars.onCompleteAllParams;
			delete $vars.stagger;
			delete $vars.onCompleteAll;
			delete $vars.onCompleteAllParams;
			for (i = 0; i < $targets.length; i++) {
				vars = {};
				for (p in $vars) {
					vars[p] = $vars[p];
				}
				group[group.length] = new $DefaultTweenClass($targets[i], $duration, vars);
			}
			if (group.stagger < 0) {
				group.progressWithDelay = 0;
			}
			return group;
		}
		
		/**
		 * Exactly the same as TweenGroup.allTo(), but instead of tweening the properties from where they're at currently to whatever you define, this tweens them the opposite way - from where you define TO where ever they are. This is handy for when things are set up on the stage the way they should end up and you just want to tween them into place.
		 * 
		 * @param $targets An Array of objects to tween.
		 * @param $duration Duration (in seconds) of the tween
		 * @param $vars An object containing the beginning values of all the properties you'd like to have tweened. Additional special properties: "stagger", "onCompleteAll", and "onCompleteAllParams"
		 * @param $DefaultTweenClass Defines which tween class to use. Choices are TweenLite, TweenFilterLite, or TweenMax.
		 * @return TweenGroup instance
		 */
		public static function allFrom($targets:Array, $duration:Number, $vars:Object, $DefaultTweenClass:Class=null):TweenGroup {
			$vars.runBackwards = true;
			return allTo($targets, $duration, $vars, $DefaultTweenClass);
		}
		

//---- PROTECTED STATIC FUNCTIONS -------------------------------------------------------------------------------

		protected static function checkExpiration($e:Event):void {
			var time:uint = TweenLite.currentTime, a:Array = _unexpired, tg:TweenGroup, i:int;
			for (i = a.length - 1; i > -1; i--) {
				tg = a[i];
				if (tg.endTime > _prevTime && tg.endTime <= time && !tg.paused) {
					a.splice(i, 1);
					tg.expired = true;
					tg.handleCompletion();
				}
			}
			_prevTime = time;
		}
		
		
//---- PROTECTED FUNCTIONS ---------------------------------------------------------------------------------------
		
		protected function realign():void {
			if (_align != ALIGN_NONE && _tweens.length > 1) {
				var l:uint = _tweens.length, i:int, offset:Number = _stagger * 1000;
				
				if (_align == ALIGN_SEQUENCE) {
					setTweenInitTime(_tweens[0], _initTime);
					for (i = 1; i < l; i++) {
						setTweenInitTime(_tweens[i], getEndTime(_tweens[i - 1]) + offset);
					}
					
				} else if (_align == ALIGN_INIT) {
					for (i = 0; i < l; i++) {
						setTweenInitTime(_tweens[i], _initTime + (offset * i));
					}
					
				} else if (_align == ALIGN_START) {
					for (i = 0; i < l; i++) {
						setTweenStartTime(_tweens[i], _startTime + (offset * i));
					}
					
				} else { //ALIGN_END
					for (i = 0; i < l; i++) {
						setTweenInitTime(_tweens[i], this.endTime - ((_tweens[i].delay + _tweens[i].duration) * 1000 / _tweens[i].combinedTimeScale) - (offset * i));
					}
				}
				
			}
			updateTimeSpan();
		}
		
		protected function offsetTime($tweens:Array, $offset:Number):void {
			if ($tweens.length != 0) {
				var ms:Number = $offset * 1000; //offset in milliseconds
				var time:Number = (isNaN(_pauseTime)) ? TweenLite.currentTime : _pauseTime;
				var tweens:Array = getRenderOrder($tweens, time);
				var isPaused:Boolean, tween:TweenLite, render:Boolean, startTime:Number, end:Number, i:int, toRender:Array = [];
				for (i = tweens.length - 1; i > -1; i--) {
					tween = tweens[i];
					tween.initTime += ms;
					isPaused = Boolean(tween.startTime == 999999999999999);
					
					startTime = tween.initTime + (tween.delay * (1000 / tween.combinedTimeScale)); //this forces paused tweens with false start times to adjust to the normal one temporarily so that we can render it properly.
					end = getEndTime(tween);
					
					render = ((startTime <= time || startTime - ms <= time) && (end >= time || end - ms >= time)); //only render what's necessary
					
					if (isNaN(_pauseTime) && end >= time) {
						tween.enabled = true; //make sure they're in the rendering queue in case they were already completed!
					}
					if (!isPaused) {
						tween.startTime = startTime;
					}
					if (startTime >= time) { //don't allow tweens with delays that haven't expired yet to be active
						if (!tween.initted) { //otherwise a TweenGroup could be paused immediately after creation, then resumed later and if there's a sequential tween of an object, initTweenVals() could get called before the previous tweens finished running, meaning if the tweens pertain to the same property, the wrong value(s) could get recorded in the tweens Array
							render = false;
						}
						tween.active = false;
					}
					if (render) {
						toRender[toRender.length] = tween;
					}
				}
				
				for (i = toRender.length - 1; i > -1; i--) {
					renderTween(toRender[i], time);
				}
				
				this.endTime += ms;
				_startTime += ms;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -