Monday, December 8, 2008

TweenEffect to an Image(Rotating)

?xml version="1.0"?>
-- effects/MainRotation.mxml -->
mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:MyComp="myEffects.*" paddingTop="150">
MyComp:Rotation id="Rotate90" angleFrom="0" angleTo="360" duration="1000" />
mx:Image source="@Embed(source='1.png')" mouseDownEffect="{Rotate90}"/>
mx:Application>


------------------Rotation.as-----------------------------------------------

package myEffects
{
// myEffects/Rotation.as
import mx.effects.TweenEffect;
import mx.effects.EffectInstance;
import mx.effects.IEffectInstance;

public class Rotation extends TweenEffect
{
// Define parameters for the effect.
public var angleFrom:Number = 0;
public var angleTo:Number = 360;

// Define constructor with optional argument.
public function Rotation(targetObj:* = null) {
super(targetObj);
instanceClass= RotationInstance;
}

// Override getAffectedProperties() method to return "rotation".
override public function getAffectedProperties():Array {
return ["rotation"];
}

// Override initInstance() method.
override protected function initInstance(inst:IEffectInstance):void {
super.initInstance(inst);
RotationInstance(inst).angleFrom = angleFrom;
RotationInstance(inst).angleTo = angleTo;
}
}
}


-------------------------------RotationInstance.as-------------------------------

package myEffects
{
// myEffects/RotationInstance.as
import mx.effects.effectClasses.TweenEffectInstance;
import mx.effects.Tween;

public class RotationInstance extends TweenEffectInstance
{
// Define parameters for the effect.
public var angleFrom:Number;
public var angleTo:Number;

public function RotationInstance(targetObj:*) {
super(targetObj);
}

// Override play() method class.
override public function play():void {
// All classes must call super.play().
super.play();
// Create a Tween object. The tween begins playing immediately.
var tween:Tween =
createTween(this, angleFrom, angleTo, duration);
}

// Override onTweenUpdate() method.
override public function onTweenUpdate(val:Object):void {
target.rotation = val;
}

// Override onTweenEnd() method.
override public function onTweenEnd(val:Object):void {
// All classes that implement onTweenEnd()
// must call super.onTweenEnd().
super.onTweenEnd(val);
}
}
}

No comments: