一、简单的缓动
一个实例名为box的正方体,开始alpha为0.5,在两秒内移动到x:300 y:100的位置,alpha变为1。
import caurina.transitions.Tweener;
Tweener.addTween(box,{x:300, y:100, alpha:1, time:2, transition:"linear"});
使用该类库,使用顺序缓动也很容易,你不需要使用事件同步你的缓动效果
下面代码实现的是在2秒内将一个物体从一个地方改变为x:300 y:100 alpha=1,并在2.5秒后1秒内旋转50°
import caurina.transitions.Tweener;
Tweener.addTween(box,{x:300, y:100, alpha:1, time:2, transition:"linear"});
Tweener.addTween(box,{rotation:50, time:1, transition:"linear",delay:2.5});
例3:下面代码实现的功能为延迟2.5秒后在一秒内旋转50°并在结束时调用函数doThis()
Tweener.addTween(box,{rotation:50, time:1, transition:"linear",delay:2.5, onComplete:doThis});
function doThis()
{
trace("here");
}
例4:下面代码实现的功能为延迟2.5秒后在一秒内旋转50°并在结束时调用函数doThis(s)
Tweener.addTween(box,{rotation:50, time:1, transition:"linear",delay:2.5, onComplete:doThis, onCompleteParams:["hello world"]});
function doThis(s:String)
{
trace(s);
}
例5:下面代码实现的功能为延迟2.5秒后在一秒内旋转50°并在结束时调用函数doThis(n1,n2,n3)
var nr1:Number=1;
var nr2:Number=2;
var nr3:Number=3;
Tweener.addTween(box,{rotation:50, time:1, transition:"linear",delay:2.5, onComplete:doThis, onCompleteParams:[nr1,nr2,nr3]});
function doThis(n1:Number, n2:Number, n3:Number)
{
trace(n1+n2+n3);
}
例六:下面实例实现五个按钮,依次从大到小显示。
import caurina.transitions.*;
for(var i:uint=0;i<5;i++){
this["mc"+i]=new mc();
this["mc"+i].x=i*60+100;
this["mc"+i].y=180;
this["mc"+i].width=200;
this["mc"+i].height=200
addChild(this["mc"+i])
this["mc"+i].alpha=0;
Tweener.addTween(this["mc"+i], { width:50,height:50,alpha: 1,y:200, time: 0.2, transition: "easeOutExpo",delay:i*0.2});
}
二、多重缓动
var my_array:Array=new Array();
var timer:Timer;
for(var i:int=0;i<10;i++)
{var clip:MovieClip=new MovieClip();
clip.graphics.beginFill(0x33FFFF,1);
clip.graphics.drawRect(0,0,35,35);
clip.graphics.endFill();
addChild(clip);
my_array.push(clip);
clip.x=35+clip.width*i+10*i;
clip.y=10;
}
timer=new Timer(300,my_array.length);
timer.addEventListener(TimerEvent.TIMER,cambiaColore);
timer.start();
function cambiaColore(evt:TimerEvent):void
{
Tweener.addTween(my_array[evt.target.currentCount-1],{_color:0x333333,time:1,transition:"easeInBounce"});
Tweener.addTween(my_array[evt.target.currentCount-1],{y:100,time:1,transition:"easeInBounce"});
}
例二:
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.getDefinitionByName;
import caurina.transitions.Tweener;
public class Main extends MovieClip
{
private var items_array:Array;
private var background_colors:Array;
private var container_mc:MovieClip;
private var underline_mc:MovieClip;
private const ROWS:int=10;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.frameRate=31;
items_array=new Array(menu_mc.item_0_mc,menu_mc.item_1_mc,menu_mc.item_2_mc,menu_mc.item_3_mc,menu_mc.item_4_mc);
background_colors=new Array(0x006699,0xCC0066,0xFF3300,0x009900,0x333333);
changeItemColor(0);
displayContent("Square");
addMenuListeners();
}
private function addMenuListeners():void
{
for(var i:int=0;i < items_array.length;i++)
{
items_array[i].id=i;
items_array[i].mouseChildren=false;
items_array[i].buttonMode=true;
items_array[i].addEventListener(MouseEvent.MOUSE_OVER,setOver);
items_array[i].addEventListener(MouseEvent.MOUSE_OUT,setOut);
items_array[i].addEventListener(MouseEvent.MOUSE_DOWN,setDown);
}
}
private function setOver(evt:MouseEvent):void
{
underline_mc=new MovieClip();
underline_mc.graphics.beginFill(0xFFFFFF,1);
underline_mc.graphics.drawRect(0,0,1,1);
underline_mc.x=evt.target.x;
underline_mc.y=evt.target.y+evt.target.height+5;
menu_mc.addChild(underline_mc);
Tweener.addTween(underline_mc,{width:evt.target.width+5,time:0.3,transition:"easeOutQuad"});
}
private function setOut(evt:MouseEvent):void
{
if(underline_mc!=null)
{
menu_mc.removeChild(underline_mc);
underline_mc=null;
}
}
private function setDown(evt:MouseEvent):void
{
Tweener.addTween(bg_mc,{_color:background_colors[evt.target.id],time:1.2,transition:"easeOutQuad"});
menu_mc.removeChild(underline_mc);
underline_mc=null;
changeItemColor(evt.target.id);
switch(evt.target.id)
{
case 0:
displayContent("Square");
break;
case 1:
displayContent("Circle");
break;
case 2:
displayContent("Star");
break;
case 3:
displayContent("Oval");
break;
case 4:
displayContent("Line");
break;
}
}
private function changeItemColor(n:int):void
{
for(var i:int=0;i < items_array.length;i++)
{
if(i==n)
{
items_array[i].mouseEnabled=false;
Tweener.addTween(items_array[i],{_color:0x000000,time:1.5,transition:"easeOutQuad"});
}
else
{
items_array[i].mouseEnabled=true;
Tweener.addTween(items_array[i],{_color:0xFFFFFF,time:1.5,transition:"easeOutQuad"});
}
}
}
private function displayContent(s:String):void
{
var className:Class=getDefinitionByName(s)as Class;
if(container_mc!=null)
{
removeChild(container_mc);
container_mc=null;
}
container_mc=new MovieClip();
for(var i:int=0;i < 50;i++)
{
var clip:MovieClip=new className();
clip.scaleX=clip.scaleY=0;
clip.x=66+((i%ROWS)*30);
clip.y=66+(Math.floor(i/ROWS)*30);
Tweener.addTween(clip,{scaleX:2,scaleY:2,delay:0.05*i,time:0.2,transition:"easeOutQuad"});
container_mc.addChild(clip);
}
container_mc.x=(stage.stageWidth-container_mc.width)/2;
container_mc.y=(stage.stageHeight-container_mc.height)/2;
addChild(container_mc);
}
}
}