forked from: リボン作ってみようと思ったけど失敗

by vlad.el.rojo forked from リボン作ってみようと思ったけど失敗 (diff: 1)
♥0 | Line 209 | Modified 2011-08-27 09:56:50 | MIT License
play

ActionScript3 source code

/**
 * Copyright vlad.el.rojo ( http://wonderfl.net/user/vlad.el.rojo )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/x0Z1
 */

// forked from oreore's リボン作ってみようと思ったけど失敗
package 
{
    import flash.display.*;
    import flash.events.*;
    
    /**
     * ...
     * @author oreore
     */
    /*
    * graphic.drawPath 使えばいけるかと思ったけど
    * やっぱり無理だった。。。。
    * 何かよい方法なないでしょうか?
    */
    public class Main extends Sprite 
    {
        private var a:Array;
        private var ballArray:Array;
        private var ballArray2:Array;
        private var s:Shape;
        private const BALL_NUM:int = 51;
        private const BALL_NUM2:int = 49;
        private var flag:int = 1;
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            var stageH:Number = stage.stageHeight;
            var stageW:Number = stage.stageWidth;
            var stageH2:Number = stage.stageHeight *.5;
            var stageW2:Number = stage.stageWidth *.5;
            
            
            _commands = new Vector.<int>();
            _data = new Vector.<Number>();
            
            s = new Shape();
            addChild(s);
            a = [];
            ballArray = [];
            ballArray2 = [];
            ballArray3 = [];
            for (var i:int = 0; i < BALL_NUM; i++) 
            {
                //var obj:Object = new Object();
                a[i] = new Ball(3, 0xff0000);;
                a[i].x = Math.random() * stageW;
                a[i].y = Math.random() * stageH;
                var ball:Ball = new Ball(2, 0xff0000);
                var ball2:Ball = new Ball(2, 0x000000);
                var ball3:Ball = new Ball(3, 0x0000ff);
                
                ballArray[i] = ball;
                ballArray2[i] = ball2;
                ballArray3[i] = ball3;
            }
            
            var n:int = BALL_NUM * 2;
            _commands[0] = 1;
            for (i = 1; i < n; i++) 
            {
                if (i == BALL_NUM - 1)
                {
                    _commands[i] = 2;
                }else if (i == n - 1) {
                    _commands[i] = 2;
                }else {
                    _commands[i] = 3;
                }
                
            }
            _commands[i] = 2;
            
            lineDraw();
            
            
            //stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            //stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            addEventListener(Event.ENTER_FRAME, onLoop);
            
            
            _fillFlag = GraphicsPathWinding.NON_ZERO;
            //stage.addEventListener(MouseEvent.CLICK, onClikc);
            
        }
        
        private function onClikc(e:MouseEvent):void 
        {
            flag = flag ^ 3;
            trace(flag);
            if (flag == 1) {
                _fillFlag = GraphicsPathWinding.NON_ZERO;
            }else {
                _fillFlag = GraphicsPathWinding.EVEN_ODD;
            }
            
        }
        
        private function onMouseUp(e:Event):void 
        {
            
            if (e.target is Ball) {
                e.target.stopDrag();
            }
        }
        
        private function onMouseDown(e:MouseEvent):void 
        {
            trace(e.target is Ball);
            if (e.target is Ball) {
                
                trace(e.target);
                e.target.startDrag();
                e.target.x = mouseX;
                e.target.y = mouseY;
            }
            
        }
        private var vx:Number = 0;
        private var vy:Number = 0;
        private var spring:Number = 0.03;
        private var furiction:Number = 0.83;
        private var ballArray3:Array;
        private var _data:Vector.<Number>;
        private var _commands:Vector.<int>;
        private var _fillFlag:String;
        
        private function onLoop(e:Event):void 
        {
            vx = mouseX;
            vy = mouseY;
            
            
            a[0].x = vx;
            a[0].y = vy;
            for (var i:int = 1; i < BALL_NUM; i++) 
            {
                var dx:Number = a[i - 1].x - a[i].x;
                var dy:Number = a[i - 1].y - a[i].y;
                var ax:Number = dx * spring;
                var ay:Number = dy * spring;
                a[i].vx += ax;
                a[i].vy += ay;
                a[i].vx *= furiction;
                a[i].vy *= furiction;
                a[i].x += a[i].vx;
                a[i].y += a[i].vy;
            }
            
            lineDraw();
            
            if (Math.abs(ax) < 0.001) {
                for (i = 1; i <BALL_NUM ; i++) 
                {
                    a[i].x = Math.random()*stage.stageWidth;
                    a[i].y = Math.random()*stage.stageHeight;
                }
            }
            
            
        }
        
        
        private function lineDraw():void
        {
            _data = new Vector.<Number>();
            graphics.clear();
            s.graphics.clear();
            graphics.beginFill(0x000000);
            
            _data.push(a[0].x);
            _data.push(a[0].y);
            
            ballArray[0].x = a[0].x;
            ballArray[0].y = a[0].y;
            
            
            for (var i:int = 1; i < BALL_NUM2; i++) 
            {
                var xc:Number = (a[i].x +a[i + 1].x) * .5;
                var yc:Number = (a[i].y +a[i + 1].y) * .5;
                
                ballArray[i].x = a[i].x;
                ballArray[i].y = a[i].y;
                ballArray2[i].x = xc;
                ballArray2[i].y = yc;
                
                _data.push(a[i].x);
                _data.push(a[i].y);
                _data.push(xc);
                _data.push(yc);
            }
            
            
            
            _data.push(a[i].x);
            _data.push(a[i].y);
            _data.push(a[i + 1].x);
            _data.push(a[i + 1].y);
            
            ballArray[i].x = a[i].x;
            ballArray[i].y = a[i].y;
            
            _data.push(a[i + 1].x + 10);
            _data.push(a[i + 1].y + 10);
            
            for (i = BALL_NUM2; i >= 1; i--) 
            {
                xc = (a[i].x +a[i - 1].x+20) * .5;
                yc = (a[i].y+a[i - 1].y + 20) * .5;
                ballArray3[i].x = xc;
                ballArray3[i].y = yc;
                
                _data.push(a[i].x + 10);
                _data.push(a[i].y + 10);
                _data.push(xc);
                _data.push(yc);
                
            }
            
            _data.push(a[i].x + 10);
            _data.push(a[i].y + 10);
            _data.push(a[i].x);
            _data.push(a[i].y);
            
            
            graphics.drawPath(_commands,_data, _fillFlag);
            graphics.endFill();
            _data = null;
        }
        
        
    }
    
}

    import flash.display.Sprite;
    
    class Ball extends Sprite{
        public var radius:Number;
        public var color:uint;
        public var vx:Number = 0;
        public var vy:Number = 0;
        public var oldX:Number = 0;
        public var oldY:Number = 0;
        public var ax:Number = 0;
        public var ay:Number = 0;
        public var mass:Number = 0;
        private var _alpha:Number = 1;
        
        public function Ball(radius:Number = 40, color:uint = 0xff6633,alphaNumber:Number= 1){
            this.radius = radius;
            this.color = color;
            this._alpha= alphaNumber
            init();
        }
        
        private function init():void{
            graphics.beginFill(color,_alpha);
            graphics.drawCircle(0,0,radius);
            graphics.endFill();
        }
        
        public function fillColor(value:uint):void
        {
            graphics.beginFill(value);
            graphics.drawCircle(0,0,radius);
            graphics.endFill();
        }
    }