Loaderをドラッグ&ドロップ
ドラッグしてみてね。
/**
* Copyright osamX ( http://wonderfl.net/user/osamX )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9oyo
*/
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.net.URLRequest;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.Bounce;
import org.libspark.betweenas3.easing.Cubic;
public class Main extends Sprite
{
private const homePoint:Point = new Point(465 / 2 - 64, 465 - 128); //loaderの初期位置(アニメーション用)
private var loader:Loader; //主役
private var isMouseDown:Boolean = false; //マウスがダウンされているかどうか
private var mouseDownPos:Point; //loader上のマウスがダウンされた位置
private var stageMouseDownPos:Point; //stage上のマウスがダウンされた位置(アニメーション用)
public function Main():void {
loadImage();
}
//Loaderで画像ロード・表示
private function loadImage():void {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, arguments.callee);
addChild(loader);
addEventListeners();
});
loader.load(new URLRequest("http://a3.twimg.com/profile_images/1199283975/twicon11w_reasonably_small.png"));//twitterのアイコン (128*128)
loader.x = homePoint.x;
loader.y = homePoint.y;
}
//イベントリスナー追加
private function addEventListeners():void {
loader.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); // loaderにMOUSE_DOWNのイベントを拾わせる
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); // stageにMOUSE_UPのイベントを拾わせる
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); // MOUSE_MOVEでドラッグ中の処理を行う
}
//MOUSE_DOWN
private function onMouseDown(e:MouseEvent):void {
isMouseDown = true;
mouseDownPos = new Point(loader.mouseX, loader.mouseY); //loader上のカーソルの座標を保存。
stageMouseDownPos = new Point(stage.mouseX, stage.mouseY); //アニメーション用
}
//MOUSE_UP
private function onMouseUp(e:MouseEvent):void {
isMouseDown = false;
backToHome();
}
//ドラッグ中の処理
private function onMouseMove(e:Event):void {
if (!isMouseDown) return;
loader.x = stage.mouseX - mouseDownPos.x;
loader.y = stage.mouseY - mouseDownPos.y;
}
//アニメーション
private function backToHome():void {
if (stageMouseDownPos.y > stage.mouseY) {
BetweenAS3.parallel(
BetweenAS3.tween(loader, { x:homePoint.x }, null, 0.5 , Cubic.easeOut),
BetweenAS3.tween(loader, { y:homePoint.y }, null, 0.5, Bounce.easeOut)
).play();
}
else {
BetweenAS3.tween(loader, { x:homePoint.x, y:homePoint.y }, null, 0.5 , Cubic.easeOut).play();
}
}
}
}