1. 程式人生 > >ActionScript 3.0巧用Tween類實現拖動物體自由落地

ActionScript 3.0巧用Tween類實現拖動物體自由落地

import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import fl.transitions.Tween;
import fl.transitions.easing.*;

//聽說shape比sprite節省資源
var circle:Sprite=new Sprite();
circle.graphics.beginFill(0);
//試著把circle.graphics.drawCircle(100,100,10)發現每次偏移,說明每次startDrag()方法會重繪
circle.graphics.drawCircle(0,0,10);
circle.graphics.endFill();
stage.addChild(circle);
var t:Tween=newTween(circle,"y",Bounce.easeOut,mouseY,stage.stageHeight-circle.height/2,5,true);
//先保持不動,等停止拖動再觸發緩動
t.stop();
circle.x=100;
circle.y=100;
circle.addEventListener(MouseEvent.MOUSE_DOWN,startdrag);
circle.addEventListener(MouseEvent.MOUSE_UP,stopdrag);
function startdrag(e:MouseEvent):void{
    t.stop();
    circle.startDrag(true);
 }
function stopdrag(e:MouseEvent):void{
    circle.stopDrag();
  t.start();
 }