📄 tweener.as
字号:
if (tTweening.waitFrames) break;
}
} while (cTime >= nv);
} else {
// It's a normal transition tween
mustUpdate = tTweening.skipUpdates < 1 || !tTweening.skipUpdates || tTweening.updatesSkipped >= tTweening.skipUpdates;
if (cTime >= tTweening.timeComplete) {
isOver = true;
mustUpdate = true;
}
if (!tTweening.hasStarted) {
// First update, read all default values (for proper filter tweening)
if (Boolean(tTweening.onStart)) {
eventScope = Boolean(tTweening.onStartScope) ? tTweening.onStartScope : tScope;
try {
tTweening.onStart.apply(eventScope, tTweening.onStartParams);
} catch(e:Error) {
handleError(tTweening, e, "onStart");
}
}
var pv:Number;
for (pName in tTweening.properties) {
if (tTweening.properties[pName].isSpecialProperty) {
// It's a special property, tunnel via the special property function
if (Boolean(_specialPropertyList[pName].preProcess)) {
tTweening.properties[pName].valueComplete = _specialPropertyList[pName].preProcess(tScope, _specialPropertyList[pName].parameters, tTweening.properties[pName].originalValueComplete, tTweening.properties[pName].extra);
}
pv = _specialPropertyList[pName].getValue(tScope, _specialPropertyList[pName].parameters, tTweening.properties[pName].extra);
} else {
// Directly read property
pv = tScope[pName];
}
tTweening.properties[pName].valueStart = isNaN(pv) ? tTweening.properties[pName].valueComplete : pv;
}
mustUpdate = true;
tTweening.hasStarted = true;
}
if (mustUpdate) {
for (pName in tTweening.properties) {
tProperty = tTweening.properties[pName];
if (isOver) {
// Tweening time has finished, just set it to the final value
nv = tProperty.valueComplete;
} else {
if (tProperty.hasModifier) {
// Modified
t = cTime - tTweening.timeStart;
d = tTweening.timeComplete - tTweening.timeStart;
nv = tTweening.transition(t, 0, 1, d, tTweening.transitionParams);
nv = tProperty.modifierFunction(tProperty.valueStart, tProperty.valueComplete, nv, tProperty.modifierParameters);
} else {
// Normal update
t = cTime - tTweening.timeStart;
b = tProperty.valueStart;
c = tProperty.valueComplete - tProperty.valueStart;
d = tTweening.timeComplete - tTweening.timeStart;
nv = tTweening.transition(t, b, c, d, tTweening.transitionParams);
}
}
if (tTweening.rounded) nv = Math.round(nv);
if (tProperty.isSpecialProperty) {
// It's a special property, tunnel via the special property method
_specialPropertyList[pName].setValue(tScope, nv, _specialPropertyList[pName].parameters, tTweening.properties[pName].extra);
} else {
// Directly set property
tScope[pName] = nv;
}
}
tTweening.updatesSkipped = 0;
if (Boolean(tTweening.onUpdate)) {
eventScope = Boolean(tTweening.onUpdateScope) ? tTweening.onUpdateScope : tScope;
try {
tTweening.onUpdate.apply(eventScope, tTweening.onUpdateParams);
} catch(e:Error) {
handleError(tTweening, e, "onUpdate");
}
}
} else {
tTweening.updatesSkipped++;
}
}
if (isOver && Boolean(tTweening.onComplete)) {
eventScope = Boolean(tTweening.onCompleteScope) ? tTweening.onCompleteScope : tScope;
try {
tTweening.onComplete.apply(eventScope, tTweening.onCompleteParams);
} catch(e:Error) {
handleError(tTweening, e, "onComplete");
}
}
return (!isOver);
}
// On delay, hasn't started, so returns true
return (true);
}
/**
* Initiates the Tweener--should only be ran once.
*/
public static function init(...rest):void {
_inited = true;
// Registers all default equations
_transitionList = new Object();
Equations.init();
// Registers all default special properties
_specialPropertyList = new Object();
_specialPropertyModifierList = new Object();
_specialPropertySplitterList = new Object();
}
/**
* Adds a new function to the available transition list "shortcuts".
*
* @param p_name String Shorthand transition name
* @param p_function Function The proper equation function
*/
public static function registerTransition(p_name:String, p_function:Function): void {
if (!_inited) init();
_transitionList[p_name] = p_function;
}
/**
* Adds a new special property to the available special property list.
*
* @param p_name Name of the "special" property.
* @param p_getFunction Function that gets the value.
* @param p_setFunction Function that sets the value.
*/
public static function registerSpecialProperty(p_name:String, p_getFunction:Function, p_setFunction:Function, p_parameters:Array = null, p_preProcessFunction:Function = null): void {
if (!_inited) init();
var sp:SpecialProperty = new SpecialProperty(p_getFunction, p_setFunction, p_parameters, p_preProcessFunction);
_specialPropertyList[p_name] = sp;
}
/**
* Adds a new special property modifier to the available modifier list.
*
* @param p_name Name of the "special" property modifier.
* @param p_modifyFunction Function that modifies the value.
* @param p_getFunction Function that gets the value.
*/
public static function registerSpecialPropertyModifier(p_name:String, p_modifyFunction:Function, p_getFunction:Function): void {
if (!_inited) init();
var spm:SpecialPropertyModifier = new SpecialPropertyModifier(p_modifyFunction, p_getFunction);
_specialPropertyModifierList[p_name] = spm;
}
/**
* Adds a new special property splitter to the available splitter list.
*
* @param p_name Name of the "special" property splitter.
* @param p_splitFunction Function that splits the value.
*/
public static function registerSpecialPropertySplitter(p_name:String, p_splitFunction:Function, p_parameters:Array = null): void {
if (!_inited) init();
var sps:SpecialPropertySplitter = new SpecialPropertySplitter(p_splitFunction, p_parameters);
_specialPropertySplitterList[p_name] = sps;
}
/**
* Starts the Tweener class engine. It is supposed to be running every time a tween exists.
*/
private static function startEngine():void {
_engineExists = true;
_tweenList = new Array();
__tweener_controller__ = new MovieClip();
__tweener_controller__.addEventListener(Event.ENTER_FRAME, Tweener.onEnterFrame);
_currentTimeFrame = 0;
updateTime();
}
/**
* Stops the Tweener class engine.
*/
private static function stopEngine():void {
_engineExists = false;
_tweenList = null;
_currentTime = 0;
_currentTimeFrame = 0;
__tweener_controller__.removeEventListener(Event.ENTER_FRAME, Tweener.onEnterFrame);
__tweener_controller__ = null;
}
/**
* Updates the time to enforce time grid-based updates.
*/
public static function updateTime():void {
_currentTime = getTimer();
}
/**
* Updates the current frame count
*/
public static function updateFrame():void {
_currentTimeFrame++;
}
/**
* Ran once every frame. It's the main engine; updates all existing tweenings.
*/
public static function onEnterFrame(e:Event):void {
updateTime();
updateFrame();
var hasUpdated:Boolean = false;
hasUpdated = updateTweens();
if (!hasUpdated) stopEngine(); // There's no tweening to update or wait, so it's better to stop the engine
}
/**
* Sets the new time scale.
*
* @param p_time Number New time scale (0.5 = slow, 1 = normal, 2 = 2x fast forward, etc)
*/
public static function setTimeScale(p_time:Number):void {
var i:Number;
var cTime:Number;
if (isNaN(p_time)) p_time = 1;
if (p_time < 0.00001) p_time = 0.00001;
if (p_time != _timeScale) {
if (_tweenList != null) {
// Multiplies all existing tween times accordingly
for (i = 0; i<_tweenList.length; i++) {
cTime = getCurrentTweeningTime(_tweenList[i]);
_tweenList[i].timeStart = cTime - ((cTime - _tweenList[i].timeStart) * _timeScale / p_time);
_tweenList[i].timeComplete = cTime - ((cTime - _tweenList[i].timeComplete) * _timeScale / p_time);
if (_tweenList[i].timePaused != undefined) _tweenList[i].timePaused = cTime - ((cTime - _tweenList[i].timePaused) * _timeScale / p_time);
}
}
// Sets the new timescale value (for new tweenings)
_timeScale = p_time;
}
}
// ==================================================================================================================================
// AUXILIARY functions --------------------------------------------------------------------------------------------------------------
/**
* Finds whether or not an object has any tweening.
*
* @param p_scope Target object.
* @return <code>true</code> if there's a tweening occuring on this object (paused, delayed, or active), <code>false</code> if otherwise.
*/
public static function isTweening (p_scope:Object):Boolean {
if (!Boolean(_tweenList)) return false;
var i:uint;
for (i = 0; i<_tweenList.length; i++) {
if (Boolean(_tweenList[i]) && _tweenList[i].scope == p_scope) {
return true;
}
}
return false;
}
/**
* Returns an array containing a list of the properties being tweened for this object.
*
* @param p_scope Target object.
* @return Total number of properties being tweened (including delayed or paused tweens).
*/
public static function getTweens (p_scope:Object):Array {
if (!Boolean(_tweenList)) return [];
var i:uint;
var pName:String;
var tList:Array = new Array();
for (i = 0; i<_tweenList.length; i++) {
if (Boolean(_tweenList[i]) && _tweenList[i].scope == p_scope) {
for (pName in _tweenList[i].properties) tList.push(pName);
}
}
return tList;
}
/**
* Returns the number of properties being tweened for a given object.
*
* @param p_scope Target object.
* @return Total number of properties being tweened (including delayed or paused tweens).
*/
public static function getTweenCount (p_scope:Object):Number {
if (!Boolean(_tweenList)) return 0;
var i:uint;
var c:Number = 0;
for (i = 0; i<_tweenList.length; i++) {
if (Boolean(_tweenList[i]) && _tweenList[i].scope == p_scope) {
c += AuxFunctions.getObjectLength(_tweenList[i].properties);
}
}
return c;
}
/* Handles errors when Tweener executes any callbacks (onStart, onUpdate, etc)
* If the TweenListObj specifies an <code>onError</code> callback it well get called, passing the <code>Error</code> object and the current scope as parameters. If no <code>onError</code> callback is specified, it will trace a stackTrace.
*/
private static function handleError(pTweening : TweenListObj, pError : Error, pCallBackName : String) : void{
// do we have an error handler?
if (Boolean(pTweening.onError) && (pTweening.onError is Function)){
// yup, there's a handler. Wrap this in a try catch in case the onError throws an error itself.
var eventScope:Object = Boolean(pTweening.onErrorScope) ? pTweening.onErrorScope : pTweening.scope;
try {
pTweening.onError.apply(eventScope, [pTweening.scope, pError]);
} catch (metaError : Error){
printError(String(pTweening.scope) + " raised an error while executing the 'onError' handler. Original error:\n " + pError.getStackTrace() + "\nonError error: " + metaError.getStackTrace());
}
} else {
// no handler, simply trace the stack trace:
if (!Boolean(pTweening.onError)){
printError(String(pTweening.scope) + " raised an error while executing the '" + pCallBackName + "'handler. \n" + pError.getStackTrace() );
}
}
}
/**
* Get the current tweening time (no matter if it uses frames or time as basis), given a specific tweening
*
* @param p_tweening TweenListObj Tween information
*/
public static function getCurrentTweeningTime(p_tweening:Object):Number {
return p_tweening.useFrames ? _currentTimeFrame : _currentTime;
}
/**
* Return the current tweener version
*
* @return String The number of the current Tweener version
*/
public static function getVersion():String {
return "AS3 1.31.71";
}
// ==================================================================================================================================
// DEBUG functions ------------------------------------------------------------------------------------------------------------------
/**
* Output an error message
*
* @param p_message String The error message to output
*/
public static function printError(p_message:String): void {
//
trace("## [Tweener] Error: "+p_message);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -