📄 gtween.as
字号:
* This will cause a tween to reverse playing visually. * There is currently an issue with this functionality for tweens with a repeat of -1 * * @param suppressEvents Indicates whether to suppress any events or callbacks that are generated as a result of the position change. **/ public function reverse(suppressEvents:Boolean=true):void { var pos:Number = repeat == -1 ? duration-_position%duration : (repeat+1)*duration-_position; if (reflect) { _reversed = ((position/duration%2>=1) == (pos/duration%2>=1)) != _reversed; } else { _reversed = !_reversed; } setPosition(pos,suppressEvents); } /** * Invalidate forces the tween to repopulate all of the initial properties from the target object, and start playing if autoplay is set to true. * If the tween is currently playing, then it will also set the position to 0. For example, if you changed the x and y position of a the target * object while the tween was playing, you could call invalidate on it to force it to resume the tween with the new property values. **/ public function invalidate():void { inited = false; if (_position > 0) { _position = 0; updatePositionOffset(); } if (autoPlay) { paused = false; } } /** * Pauses the tween by stopping tick from being automatically called. This also releases the tween for garbage collection if * it is not referenced externally. **/ public function pause():void { paused = true; } /** * Plays a tween by incrementing the position property each frame. This also prevents the tween from being garbage collected while it is active. * This is achieved by way of two methods:<br/> * 1. If the target object is an IEventDispatcher, then the tween will subscribe to a dummy event using a hard reference. This allows * the tween to be garbage collected if its target is also collected, and there are no other external references to it.<br/> * 2. If the target object is not an IEventDispatcher, then the tween is placed in the activeTweens list, to prevent collection until it is paused or reaches the end of the transition). * Note that pausing all tweens via the GTween.pauseAll static property will not free the tweens for collection. **/ public function play():void { paused = false; } /** * Jumps the tween to its beginning. This is the same as setting <code>position=-delay</code>. **/ public function beginning():void { setPosition(-_delay); } /** * Jumps the tween to its end. This is the same as setting <code>position=(repeat+1)*duration</code>. **/ public function end():void { setPosition( (repeat == -1) ? duration : (repeat+1)*duration ); } /** * Allows you to tween objects that require re-assignment whenever they are modified by reassigning the target object to a specified property of another * object. For example, in order for changes to a colorTransform object to be visible, * it must be assigned back to the <code>.transform.colorTransform</code> property of a display object. To make this work, you would call * <code>myTween.setAssignment(myDisplayObject.transform,"colorTransform");</code> * This will also cause GTween to retrieve the target each time it copies its initial values. * <br/><br/> * <b>Note:</b> this does not work with filters, as they must be assigned to an array first, and then to the filters property. Use GTweenFilter instead. * * @param assignmentTarget The object to reassign the property on. * @param assignmentProperty The name of the property to reassign the target to. **/ public function setAssignment(assignmentTarget:Object=null, assignmentProperty:String=null):void { this.assignmentTarget = assignmentTarget; this.assignmentProperty = assignmentProperty; inited = false; } /** * Sets the position of the tween. Using the position property will always suppress events and callbacks, whereas the * setPosition method allows you to manually set the position and specify whether to suppress events or not. * * @param value The position to jump to in seconds or frames (depending on the timingMode). * @param suppressEvents Indicates whether to suppress events and callbacks generated from the change in position. **/ public function setPosition(position:Number,suppressEvents:Boolean=true):void { _previousPosition = _position; _position = position; if (!inTick && !paused) { updatePositionOffset(); } var maxPos:Number = (repeat+1)*duration; var tp:Number; if (position < 0) { tp = _reversed ? duration : 0; } else if (repeat == -1 || position < maxPos) { tp = position%duration; if ((reflect && position/duration%2>=1) != _reversed) { tp = duration-tp; } } else { tp = ((reflect && repeat%2>=1) != _reversed) ? 0 : duration; } if (tp == _tweenPosition) { return; } _previousTweenPosition = _tweenPosition; _tweenPosition = tp; if (!suppressEvents && hasEventListener(Event.CHANGE)) { dispatchEvent(new Event(Event.CHANGE)); } if (!inited && _previousPosition <= 0 && _position >= 0) { init(); if (!suppressEvents && hasEventListener(Event.INIT)) { dispatchEvent(new Event(Event.INIT)); } } updateProperties(); if (repeat != -1 && _previousPosition < maxPos && position >= maxPos) { if (!suppressEvents && hasEventListener(Event.COMPLETE)) { dispatchEvent(new Event(Event.COMPLETE)); } paused = true; if (nextTween) { nextTween.paused = false; } } } // private methods // copies the initial target properties into the local startValues store. /** @private **/ protected function init():void { inited = true; if (_lockStartProperties) { return; } startValues = {}; if (assignmentTarget && assignmentProperty) { _propertyTarget = assignmentTarget[assignmentProperty]; } for (var n:String in endValues) { if (autoRotation && rotationProperties[n]) { var r:Number = endValues[n] = endValues[n] %360; var tr:Number = _propertyTarget[n] %360; startValues[n] = tr + ((Math.abs(tr-r) < 180) ? 0 : (tr>r) ? -360 : 360); } else { startValues[n] = _propertyTarget[n]; } } } // logic that runs each frame. Calculates eased position, updates properties, and reassigns the target if an assignmentTarget was set. /** @private **/ protected function updateProperties():void { var ratio:Number = ease(_tweenPosition/duration, 0, 1, 1); for (var n:String in endValues) { updateProperty(n,startValues[n],endValues[n],ratio); } if (autoVisible && "alpha" in endValues && "alpha" in _propertyTarget && "visible" in _propertyTarget) { _propertyTarget.visible = _propertyTarget.alpha > 0; } if (assignmentTarget && assignmentProperty) { assignmentTarget[assignmentProperty] = _propertyTarget; } } // updates a single property. Mostly for overriding. /** @private **/ protected function updateProperty(property:String, startValue:Number, endValue:Number, tweenRatio:Number):void { var value:Number = startValue+(endValue-startValue)*tweenRatio; if (snapping && snappingProperties[property]) { value = Math.round(value); } if (property == "currentFrame") { _propertyTarget.gotoAndStop(value<<0); } else { _propertyTarget[property] = value; } } // locks or unlocks the tween in memory. /** @private **/ protected function setGCLock(value:Boolean):void { if (value) { if (_target is IEventDispatcher) { _target.addEventListener("GDS__NONEXISTENT_EVENT", nullListener,false,0,false); } else { activeTweens[this] = true; } } else { if (_target is IEventDispatcher) { _target.removeEventListener("GDS__NONEXISTENT_EVENT", nullListener); } delete(activeTweens[this]); } } // copies an object's dynamic properties. /** @private **/ protected function copyObject(o:Object):Object { var copy:Object = {}; for (var n:String in o) { copy[o] = o[n]; } return copy; } // updates the current positionOffset based on the current ticker position. /** @private **/ protected function updatePositionOffset():void { positionOffset = ticker.position-_position; } // empty listener used by setGCLock. /** @private **/ protected function nullListener(evt:Event):void {}; // handles tick events while playing. /** @private **/ protected function handleTick(evt:Event):void { inTick = true; if (pauseAll) { updatePositionOffset(); } else { setPosition(ticker.position - positionOffset, false); } inTick = false; } } }import flash.events.IEventDispatcher;interface ITicker extends IEventDispatcher { function get position():Number;}import flash.utils.getTimer;import flash.utils.Timer;import flash.events.EventDispatcher;import flash.events.Event;import flash.events.TimerEvent;class TimeTicker extends EventDispatcher implements ITicker { protected var timer:Timer; public function TimeTicker():void { timer = new Timer(20); timer.start(); timer.addEventListener(TimerEvent.TIMER,tick); } public function get position():Number { return getTimer()/1000; } public function set interval(value:Number):void { timer.delay = value*1000; } protected function tick(evt:TimerEvent):void { dispatchEvent(new Event("tick")); evt.updateAfterEvent(); }}import flash.display.Shape;class FrameTicker extends EventDispatcher implements ITicker { protected var shape:Shape; protected var _position:Number=0; public function FrameTicker():void { shape = new Shape(); shape.addEventListener(Event.ENTER_FRAME,tick); } public function get position():Number { return _position; } protected function tick(evt:Event):void { _position++; dispatchEvent(new Event("tick")); }}class HybridTicker extends EventDispatcher implements ITicker { protected var shape:Shape; public function HybridTicker():void { shape = new Shape(); shape.addEventListener(Event.ENTER_FRAME,tick); } public function get position():Number { return getTimer()/1000; } protected function tick(evt:Event):void { dispatchEvent(new Event("tick")); }}import flash.utils.Proxy;import flash.utils.flash_proxy;import com.gskinner.motion.GTween;dynamic class TargetProxy extends Proxy { private var gTween:GTween; public function TargetProxy(gTween:GTween):void { this.gTween = gTween; } // proxy methods: flash_proxy override function callProperty(methodName:*, ...args:Array):* { return gTween.propertyTarget[methodName].apply(null,args); // GDS: propertyTarget. } flash_proxy override function getProperty(prop:*):* { var value:Number = gTween.getProperty(prop); return (isNaN(value)) ? gTween.propertyTarget[prop] : value; } flash_proxy override function setProperty(prop:*,value:*):void { if (isNaN(value)) { gTween.propertyTarget[prop] = value; } else { gTween.setProperty(String(prop), Number(value)); } } flash_proxy override function deleteProperty(prop:*):Boolean { return gTween.deleteProperty(prop); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -