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

ぎゅいーーん forked from: 吸引力

お絵かきして、ダブルクリックで吸引
/**
 * Copyright ton ( http://wonderfl.net/user/ton )
 * GNU General Public License, v3 ( http://www.gnu.org/licenses/quick-guide-gplv3.html )
 * Downloaded from: http://wonderfl.net/c/axov
 */

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    
    public class Suitori extends Sprite
    {
        private var line_data:Array;
        private var move_speed_data:Array;
        private var move_data:Array;
        private var mouse_down:Boolean;
        private var target_y:Number;
        private var target_x:Number;
        private var speed:Number=0;
        private var cnt:int;
        private var _sucking:Boolean;
        public function Suitori()
        {
            this.graphics.lineStyle(3);
            
            
            line_data=new Array();
            move_data=new Array();
            move_speed_data=new Array();
            move_data=new Array();
            stage.addEventListener(MouseEvent.MOUSE_DOWN,_mouseDown);
            stage.addEventListener(MouseEvent.MOUSE_MOVE,_mouseMove);
            stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
            stage.doubleClickEnabled = true;
            stage.addEventListener(MouseEvent.DOUBLE_CLICK,sucking_event);

        }
        private function sucking_event(event:MouseEvent):void{
            if(!_sucking){
            target_x= mouseX;
            target_y= mouseY;
            sucking(target_x,target_y);
            _sucking=true;
            }
        }
        private function btn(label:String):Sprite{
            var spr:Sprite=new Sprite();
            var label_txt:TextField=new TextField();
            label_txt.selectable=false;
            label_txt.mouseEnabled=false;
            label_txt.text=label;
            label_txt.autoSize=TextFieldAutoSize.LEFT;
            spr.addChild(label_txt);
            spr.graphics.beginFill(0xcccccc);
            spr.graphics.drawRect(0,0,label_txt.width,label_txt.height);
            spr.graphics.endFill();
            return spr;
        }
        private function _mouseDown(event:MouseEvent):void{
            if(!_sucking){
            this.graphics.moveTo(mouseX,mouseY);
            line_data.push("s");
            line_data.push([mouseX,mouseY]);
            move_data.push([mouseX,mouseY]);
            move_data.push([mouseX,mouseY]);
            mouse_down=true;
            }
        }
        private function _mouseMove(event:MouseEvent):void{
            if(mouse_down){
                this.graphics.lineTo(mouseX,mouseY);
                line_data.push([mouseX,mouseY]);
                move_data.push([mouseX,mouseY]);
            }
        }
        private function _mouseUp(event:MouseEvent):void{
            mouse_down=false;
        }
        private function sucking(target_x:Number,target_y:Number):void{
            for(var i:int;i<line_data.length;i++){
                if(line_data[i]!=="s"){
                    move_speed_data.push([Math.cos(Math.atan2(target_y-line_data[i][1],target_x-line_data[i][0]))*speed,Math.sin(Math.atan2(target_y-line_data[i][1],target_x-line_data[i][0]))*speed]);
                }
            }
            addEventListener(Event.ENTER_FRAME,_enterFrame);
        }
        private function _enterFrame(event:Event):void{
            cnt = 0;

            for(var i:int;i<line_data.length;i++){
                if(line_data[i]!=="s"){
                    move_speed_data[i]=[Math.cos(Math.atan2(target_y-line_data[i][1],target_x-line_data[i][0]))*speed,Math.sin(Math.atan2(target_y-line_data[i][1],target_x-line_data[i][0]))*speed]
                    if(Math.abs(move_data[i][0]-target_x)+Math.abs(move_data[i][1]-target_y)>speed){
                        move_data[i][0]+=move_speed_data[i][0];
                        move_data[i][1]+=move_speed_data[i][1];
                    }else{
                        cnt++;
                    }
                }else{
                    cnt++;
                }
            }
            speed+=0.3;
            this.graphics.clear();
            this.graphics.lineStyle(3);
            for(i=0;i<line_data.length;i++){
                if(line_data[i]=="s"){
                    i++;
                    this.graphics.moveTo(move_data[i][0],move_data[i][1]);
                }else{
                    this.graphics.lineTo(move_data[i][0],move_data[i][1]);
                }
            }
            if (cnt == line_data.length) {
                this.graphics.clear();
                this.graphics.lineStyle(3);                
                cnt=0;
                removeEventListener(Event.ENTER_FRAME,_enterFrame);
                move_data=[];
                line_data=[];
                move_speed_data=[];
                speed=0;
                _sucking=false;
            }
        }
    }
}