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

Soundropインスパイア系

Soundropが楽しいので作ってみました。

ドラッグして線が引けます。
線はダブルクリックで消えます。
ドラッグで出現ボールの位置は移動できます。

音は出ません。音は出ませんよ。
大事なことなので2度言いました。
Get Adobe Flash player
by kawamura 05 Apr 2011
    Embed
/**
 * Copyright kawamura ( http://wonderfl.net/user/kawamura )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/vnJO
 */

package {
    import flash.display.MovieClip;
    import flash.display.Graphics;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.utils.setInterval;
    import org.si.sion.*;
    //import net.hires.debug.Stats;
    [SWF(width = "465", height = "465", backgroundColor = "0xFFFFFF", frameRate = "60")]
    public class Main extends MovieClip {
        private var driver:SiONDriver = new SiONDriver();
        private var ax:Number=0;
        private var ay:Number=0.1
        private var vs:Number=1;
        private var rs:Number=90*Math.PI/180;
        private var l_ar:Array;
        private var lines_ar:Array=new Array();
        private var l_mc:MovieClip;
        private var l_g:Graphics;
        private var body_mc:MovieClip=new MovieClip()
        private var press_mc:MovieClip=new MovieClip()
        private var press_g:Graphics=press_mc.graphics;
        private var lines_mc:MovieClip=new MovieClip();
        private var hole_mc:MovieClip=new MovieClip();
        private var hoge_g:Graphics=hole_mc.graphics;
        private var ball_mc:MovieClip=new MovieClip();
        private var d_mc:MovieClip;
        private var L:Number=1;
        private var lcnt:Number=0;
        public function Main () {
            //stage.scaleMode = StageScaleMode.NO_SCALE;
            //stage.align = StageAlign.TOP_LEFT;
            /*
            var stats=new Stats();
            addChild( stats );
            */
            addChild(body_mc)
            press_g.beginFill(0xFF0000,0)
            press_g.drawRect(0,0,stage.stageWidth,stage.stageHeight);
            body_mc.addChild(press_mc);
            press_mc.addEventListener(MouseEvent.MOUSE_DOWN,l_mdwn);
            body_mc.addChild(lines_mc);
            hole_mc.buttonMode=true
            hoge_g.beginFill(0x000000);
            hoge_g.drawCircle(0,0,5)
            body_mc.addChild(hole_mc)
            hole_mc.x=100+(stage.stageWidth-200)*Math.random();
            hole_mc.y=20;
            
            hole_mc.addEventListener(MouseEvent.MOUSE_DOWN,hole_mdwn);
            addChild(ball_mc);
            // constructor code
            randamNewLine();
            //
            stage.addEventListener(Event.RESIZE,resz)
            startMC();
            var id:uint = setInterval(startMC,1000);
        }
        private function resz( event:Event):void{
            press_mc.width=stage.stageWidth;
            press_mc.height=stage.stageHeight;
        }

        private function l_mdwn (event:MouseEvent):void {
            var px:Number = mouseX;
            var py:Number = mouseY;
            l_ar=new Array();
            l_ar.push ([px,py]);
            l_mc=new MovieClip();
            l_mc.cnt = lcnt++;
            //
            l_g = l_mc.graphics;
            lines_mc.addChild (l_mc);
            l_g.beginFill (0x0);
            l_g.drawCircle (px,py,2);
            //
            l_mc.addEventListener (Event.ENTER_FRAME,l_ent);
            stage.addEventListener (MouseEvent.MOUSE_UP,l_mup);
        }

        private function l_mup (event:MouseEvent):void {
            var p0x:Number = l_ar[0][0];
            var p0y:Number = l_ar[0][1];
            var px:Number = mouseX;
            var py:Number = mouseY;
            l_ar.push ([px,py]);
            l_ar.push (l_mc);
            lines_ar.push (l_ar);

            l_mc.removeEventListener (Event.ENTER_FRAME,l_ent);
            l_g.clear ();
            //
            l_g.lineStyle ( 1 , 0x000000 );
            l_g.moveTo ( p0x , p0y);
            l_g.lineTo (px,py);
            l_g.endFill ();
            l_g.beginFill (0x0);
            l_g.drawCircle (px,py,2);

            l_g.beginFill (0x0);
            l_g.drawCircle (p0x,p0y,2);
            //
            stage.removeEventListener (MouseEvent.MOUSE_UP,l_mup);
            //
            l_mc.doubleClickEnabled = true;
            l_mc.buttonMode = true;
            l_mc.addEventListener (MouseEvent.DOUBLE_CLICK,l_dclk);
        }
        private function randamNewLine():void{
            var p0x:Number = hole_mc.x-50-50*Math.random();
            var p0y:Number = 350+100*Math.random();
            var px:Number =hole_mc.x+50+50*Math.random();
            var py:Number =350+100*Math.random();
            l_mc = new MovieClip()
            l_mc.cnt = lcnt++;
            //
            l_g = l_mc.graphics;
            lines_mc.addChild (l_mc);
            
            l_g.lineStyle ( 1 , 0x000000 );
            l_g.moveTo ( p0x , p0y);
            l_g.lineTo (px,py);
            l_g.endFill ();
            l_g.beginFill (0x000000);
            l_g.drawCircle (px,py,2);

            l_g.beginFill (0x000000);
            l_g.drawCircle (p0x,p0y,2);
            
            l_ar=[[p0x ,p0y] , [ px , py ] , l_mc ]
            lines_ar.push( l_ar );
            //
            l_mc.doubleClickEnabled = true;
            l_mc.buttonMode = true;
            l_mc.addEventListener (MouseEvent.DOUBLE_CLICK,l_dclk);
        }
        private function l_dclk (event:MouseEvent):void {
            var _mc:MovieClip = event.currentTarget as MovieClip;
            var i:uint;
            var n:uint = lines_ar.length
            for (i=0; i<n ; i++) {
                if (_mc.cnt == lines_ar[i][2].cnt) {
                    _mc.addEventListener (MouseEvent.DOUBLE_CLICK,l_dclk);
                    lines_mc.removeChild (_mc);
                    lines_ar.splice (i,1);
                    break;
                }
            }
        }
        private function l_ent (event:Event):void {
            l_g.clear ();
            var px:Number = l_ar[0][0];
            var py:Number = l_ar[0][1];
            l_g.beginFill (0x0);
            l_g.drawCircle (px,py,2);
            l_g.lineStyle (1,0xCCCCCC,0.5);
            l_g.moveTo (px,py);
            l_g.lineTo (mouseX,mouseY);
        }

        private function hole_mdwn (event:MouseEvent):void {
            d_mc = event.currentTarget as MovieClip;
            d_mc.startDrag ();
            stage.addEventListener (MouseEvent.MOUSE_UP,hole_mup);
        }
        private function hole_mup (event:MouseEvent):void {
            d_mc.stopDrag ();
            stage.removeEventListener (MouseEvent.MOUSE_UP,hole_mup);
        }
        private function startMC ():void {
            var _mc:MovieClip=new MovieClip();
            var g:Graphics = _mc.graphics;
            g.beginFill (0x000000);
            g.drawCircle (0,0,2);
            ball_mc.addChild (_mc);
            _mc.x = hole_mc.x;
            _mc.y = hole_mc.y;
            _mc.prex = _mc.x;
            _mc.prey = _mc.y;
            _mc.vx = vs * Math.cos(rs);
            _mc.vy = vs * Math.sin(rs);
            _mc.addEventListener (Event.ENTER_FRAME,ent);
        }

        private function ent ( event:Event ):void {
            //trace("ent")
            var i:uint;
            var n:uint = lines_ar.length;
            var _mc:MovieClip = MovieClip( event.currentTarget );
            _mc.vx +=  ax;
            _mc.vy +=  ay;
            _mc.x +=  _mc.vx;
            _mc.y +=  _mc.vy;
            //trace(_mc.x,_mc.prex);
            for (i=0; i<n; i++) {
                checkClossLine (lines_ar[i],_mc,i);
            }
            if (_mc.y > stage.stageHeight || _mc.x > stage.stageWidth || _mc.x < 0) {
                _mc.removeEventListener (Event.ENTER_FRAME,ent);
                ball_mc.removeChild (_mc);
            }
            _mc.prex = _mc.x;
            _mc.prey = _mc.y;
        }

        private function checkClossLine( _ar:Array , _mc:MovieClip , num:uint ):void{
            var p0x:Number = _ar[0][0];
            var p0y:Number = _ar[0][1];
            var p1x:Number = _ar[1][0];
            var p1y:Number = _ar[1][1];
            var l0x:Number = _mc.prex;
            var l0y:Number = _mc.prey;
            var l1x:Number = _mc.x;
            var l1y:Number = _mc.y;
            //
            var lx:Number = l1x-l0x;
            var ly:Number = l1y-l0y;
            var lr:Number = Math.atan2(ly,lx);
            //
            var s1:Number = (((l1x-l0x)*(p0y-l0y))-((l1y-l0y)*(p0x-l0x)))/2;
            var s2:Number = (((l1x-l0x)*(l0y-p1y))-((l1y-l0y)*(l0x-p1x)))/2;
            //
            var c0x:Number = p0x + (p1x-p0x)*(s1/(s1 + s2));
            var c0y:Number = p0y + (p1y-p0y)*(s1/(s1 + s2));
            //
            var d1ab:Number=Math.pow(l0x-l1x,2)+Math.pow(l0y-l1y,2);
            var d1a:Number=Math.pow(l0x-c0x,2)+Math.pow(l0y-c0y,2);
            var d1b:Number=Math.pow(l1x-c0x,2)+Math.pow(l1y-c0y,2);
            if (d1ab>d1a&&d1ab>d1b&&d1a>0) {
                var d2ab:Number=Math.pow(p0x-p1x,2)+Math.pow(p0y-p1y,2);
                var d2a:Number=Math.pow(p0x-c0x,2)+Math.pow(p0y-c0y,2);
                var d2b:Number=Math.pow(p1x-c0x,2)+Math.pow(p1y-c0y,2);
                if (d2ab > d2a && d2ab > d2b) {
                    //衝突したところ hit point
                    //driver.play("c");
                    //
                    var gai_l0:Number = (p1x-p0x)*(l0y-p0y)-(l0x-p0x)*(p1y-p0y);
                    var pl0:Number = gai_l0/Math.abs(gai_l0)
                    var gai_l1:Number = (p1x-p0x)*(l1y-p0y)-(l1x-p0x)*(p1y-p0y);
                    var pl1:Number = gai_l1/Math.abs(gai_l1)
                    //
                    var vx:Number = p1x-p0x;
                    var vy:Number = p1y-p0y;
                    var R:Number = Math.atan2(vy,vx)
                    var h1x:Number = c0x+L*Math.cos(R-0.5*Math.PI)*pl1;
                    var h1y:Number = c0y+L*Math.sin(R-0.5*Math.PI)*pl1;
                    var h1r:Number = Math.atan2(h1y-c0y,h1x-c0x);
                    var dl1r:Number = Math.atan2(l0y-c0y,l0x-c0x)-h1r
                    var H1r:Number = h1r-dl1r;
                    
                    var v:Number=Math.sqrt(Math.pow(_mc.vx,2)+Math.pow(_mc.vy,2))

                    _mc.vx=v*Math.cos(H1r);
                    _mc.vy=v*Math.sin(H1r);
                    _mc.x=_mc.prex;                
                    _mc.y=_mc.prey;
                    _mc.prex=_mc.x;
                    _mc.prey=_mc.y;
                } 
            } 
        }
    }
}