[AS3/AS2] TweenPlugin is the base class for all TweenLite and TweenMax plugins, but generally isn't used directly.
To create your own plugin, extend TweenPlugin and override whichever methods you need. Typically, you only need to override
_onInitTween() (which is called when the tween renders for the first time) and
setRatio() (which is called on every update and passes a progress parameter which is typically a value between 0 and 1, but changes according to the ease). I'd recommend looking at a simple plugin like ScalePlugin and using it as a template of sorts. There are a few key concepts to keep in mind:
- Pass the TweenPlugin constructor a comma-delimited list of property names that the plugin should overwrite, the first of which should be the property name that the plugin intercepts. For example, the ScalePlugin handles any tweens of
"scale"and it also overwrites other concurrent tweens that are handling the "scale", "scaleX", and/or "scaleY" properties of the target. Therefore, in ScalePlugin's constructor, we'd call
super("scale,scaleX,scaleY"). The first name in the list must be unique - two plugins cannot handle the same primary property.
- When a tween that uses your plugin initializes its tween values (normally when it renders the first time), a new instance of your plugin will be created and the
_onInitTween()method is called. That's where you'll want to record any initial values and prepare for the tween.
_onInitTween()should return a Boolean value that essentially indicates whether or not the plugin initted successfully. If you return false, TweenLite/Max will just use a normal tween for the value, ignoring the plugin for that particular tween. For example, maybe your tween only works with MovieClips, so if the target isn't a MovieClip you could return
setRatio()method will be called on every frame during the course of the tween and it will be passed a single parameter that's a multiplier (typically between 0 and 1, according to the ease) describing the total amount of change from the beginning of the tween (0). It will be zero at the beginning of the tween and 1 at the end, but inbetween it could be any value based on the ease applied (for example, an
ElasticOutease would cause the value to shoot past 1 and back again before the end of the tween). So if the tween uses the
Linear.ease, when it's halfway finished, the
setRatio()will receive a parameter of 0.5.
_overwritePropsis an array that should contain the properties that your plugin should overwrite in
"auto"mode. For example, the
autoAlphaplugin controls the
"alpha"properties of an object, so if another tween is created that controls the
alphaof the target object, your plugin's
_kill()method will be called which should handle killing the
"alpha"part of the tween. It is your responsibility to populate (and depopulate) the
_overwritePropsArray. Failure to do so properly can cause odd overwriting behavior.
- There's a
_roundProps()method that gets called by the RoundPropsPlugin if the user requests that certain properties get rounded to the nearest integer. If you use
_addTween()method to add property tweens, rounding will happen automatically (if necessary), but if you don't use
_addTween()and prefer to manually calculate tween values in your
setRatio()method, just remember to override the
_roundProps()method if that makes sense in your plugin (some plugins wouldn't need to accommodate rounding, like color plugins).
- If you need to run a function when the tween gets disabled, add an
_onDisable()method (named exactly that) to your plugin. It will automatically be called when the tween gets disabled (typically when it finishes and is removed from its parent timeline). Same for
_onEnable()if you need to run code when a tween is enabled. These methods should return a Boolean value indicating whether or not they changed any properties on the target becaues if so (
true), it helps notify any initting tweens of the same target to re-init. It is very rare that an
_onEnable()method is necessary, but it can be useful for things like MotionBlurPlugin which must do some very advanced things, hiding the target, changing its alpha to almost 0, etc. only while the tween occurs. If another alpha tween of that same target overwrites an existing motionBlur of the same target, the alpha would be at the wrong value normally, but the if the
true, it would force the new tween to re-init AFTER the alpha was fixed inside the
_onDisable(). Again, this is VERY rare.
- Please use the same naming convention as the rest of the plugins, like MySpecialPropertyNamePlugin.
- If you are handling more than one property in your plugin (like RoundPropsPlugin or ShortRotationPlugin), make sure you override the
_kill()method which will be passed a
varsparameter with properties that need to be killed (typically for overwriting).