In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ドラッグ&ドロップ可能なオブジェクト

複数画像を読み込む処理はコレでよいのだろうか・・・?
Get Adobe Flash player
by Droid86 01 Jan 2011
    Embed
package {

    import flash.display.Sprite;

    [SWF(frameRate = "60", backgroundColor = "#FFFFFF")]
    public class Main extends Sprite {

        public function Main():void {
            var ddObj:DragDropObject = new DragDropObject();
            ddObj.x = ddObj.y = 200;
            addChild(ddObj);
        }

    }

}


    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;

    /**
     * Drag&Drop可能なオブジェクト
     */
    class DragDropObject extends Sprite {

        public function DragDropObject():void {
            var img:ImageProcessor = new ImageProcessor();
            img.alpha = 1.0;
            img.addEventListener(MouseEvent.MOUSE_DOWN, _pickup);
            img.addEventListener(MouseEvent.MOUSE_UP, _place);
            img.addEventListener(MouseEvent.MOUSE_OVER, _handCursor);
            img.addEventListener(MouseEvent.MOUSE_OUT, _arrowCursor);
            addChild(img);
        }

        // ドラッグ処理を開始し影フィルターを付ける
        private function _pickup(e:MouseEvent):void {
            e.target.startDrag();
            e.target.filters = [new DropShadowFilter()];
            e.target.showMask();
            // 最前面に表示されるようにする
            setChildIndex(DisplayObject(e.target), numChildren - 1);
        }

        // ドラッグ処理を終了し影フィルターを外す
        private function _place(e:MouseEvent):void {
            e.target.stopDrag();
            e.target.filters = null;
            e.target.hideMask();
        }

        // 指差しハンドポインタ表示を有効にする
        private function _handCursor(e:MouseEvent):void {
            var target:Sprite = e.currentTarget as Sprite;
            target.buttonMode = true;
            target.useHandCursor = true;
        }

        // 指差しハンドポインタ表示を無効にする
        private function _arrowCursor(e:MouseEvent):void {
            var target:Sprite = e.currentTarget as Sprite;
            target.buttonMode = false;
            target.useHandCursor = false;
        }

    }


    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;

    /**
     * 画像を加工する
     */
    class ImageProcessor extends Sprite {

        // メイン画像の大きさ
        private static const SIZE:int = 100;

        // メイン画像に対するマスク画像の大きさ
        private static const RATE:Number = 0.6;

        // マスク画像の透過率
        private static const ALPHA_RATE:Number = 0.5;

        // マスク画像
        private var _maskBitmap:Bitmap;

        private var _loader:ImageLoader;

        public function ImageProcessor():void {
            _loader = new ImageLoader();
            _loader.addEventListener(Event.COMPLETE, _completed);
            _loader.load();
        }

        private function _completed(e:Event = null):void {
            _scaling(_loader.images[0]);
            _createMask(_loader.images[1]);
        }

        private function _scaling(temp:BitmapData):void {
            var scale:Number = 0;
            if (temp.width < temp.height) {
                scale = SIZE / temp.height;
            } else {
                scale = SIZE / temp.width;
            }

            var matrix:Matrix = new Matrix();
            matrix.scale(scale, scale);

            var bd:BitmapData = new BitmapData(scale * temp.width, scale * temp.height);
            bd.draw(temp, matrix);

            var bitmap:Bitmap = new Bitmap(bd, "auto", true);
            bitmap.smoothing = true;
            bitmap.x = bitmap.y = -SIZE / 2;
            addChild(bitmap);
        }

        private function _createMask(temp:BitmapData):void {
            var scale:Number = 0;
            if (temp.width < temp.height) {
                scale = SIZE * RATE / temp.height;
            } else {
                scale = SIZE * RATE / temp.width;
            }

            var matrix:Matrix = new Matrix();
            matrix.scale(scale, scale);

            var bd:BitmapData = new BitmapData(scale * temp.width, scale * temp.height, true, 0x000000);
            bd.draw(temp, matrix);

            _maskBitmap = new Bitmap(bd, "auto", true);
            _maskBitmap.alpha = ALPHA_RATE;
            _maskBitmap.smoothing = true;
            _maskBitmap.x = _maskBitmap.y = -SIZE * RATE / 2;
        }

        // マスク画像を表示にする
        public function showMask():void {
            if (contains(_maskBitmap)) {
                removeChild(_maskBitmap);
            }
            addChild(_maskBitmap);
        }

        // マスク画像を非表示にする
        public function hideMask():void {
            if (contains(_maskBitmap)) {
                removeChild(_maskBitmap);
            }
        }

    }


    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;

    /**
     * 外部画像をBitmap化する(複数画像)
     */
    class ImageLoader extends EventDispatcher {

        // 画像ファイルのURL
        private static const URL:Array = ["http://assets.wonderfl.net/images/related_images/7/7f/7f09/7f0999e96627fe7971c025d67839fe2d0d0d4091", "http://assets.wonderfl.net/images/related_images/e/e5/e508/e50817afd0b17b72eb6a0c023172b61293f80eb6"];

        // BitmapDataの配列
        private var _images:Vector.<BitmapData> = new Vector.<BitmapData>();

        // 画像ファイルの数
        private var _imagesNum:int;

        private var _loadedA:int;
        private var _loaderAList:Array;
        private var _loadedB:int;
        private var _loaderBList:Array;

        public function ImageLoader():void {
        }

        public function load():void {
            _imagesNum = URL.length;
            _loadedA = _loadedB = 0;
            _loaderAList = [];
            _loaderBList = [];

            for (var i:int = 0; i < _imagesNum; i++) {
                var loader:Loader = new Loader();
                _loaderAList[i] = loader;
                loader.contentLoaderInfo.addEventListener(Event.INIT, loaderALoaded);
                loader.load(new URLRequest(URL[i]), new LoaderContext(true));
            }
        }

        private function loaderALoaded(e:Event):void {
            e.target.removeEventListener(Event.INIT, loaderALoaded);
            _loadedA++;

            if (_loadedA == _imagesNum) {
                for (var i:int = 0; i < _imagesNum; i++) {
                    var loader:Loader = new Loader();
                    _loaderBList[i] = loader;
                    loader.contentLoaderInfo.addEventListener(Event.INIT, loaderBLoaded);
                    loader.loadBytes(_loaderAList[i].contentLoaderInfo.bytes);
                }
            }
        }

        private function loaderBLoaded(e:Event):void {
            e.target.removeEventListener(Event.INIT, loaderBLoaded);
            _loadedB++;

            if (_loadedB == _imagesNum) {
                for (var i:int = 0; i < _imagesNum; i++) {
                    var bitmapData:BitmapData = new BitmapData(_loaderBList[i].width, _loaderBList[i].height);
                    bitmapData.draw(_loaderBList[i]);
                    _images.push(bitmapData);
                }

                // 読み込み完了イベント送出
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }

        public function get images():Vector.<BitmapData> {
            return this._images;
        }

    }