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

昔持っていたオモチャの動きをASで

子供頃に緩やかな坂道が上下に並んでいたオモチャが有って
それが好きで、ずーっとビー玉を転がして遊んでいました。
今回はその動きに近いものをASで表現してみました。
Get Adobe Flash player
by yuugurenote 01 Aug 2011
    Embed
/**
 * Copyright yuugurenote ( http://wonderfl.net/user/yuugurenote )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/p7ir
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.display.DisplayObject;
    [SWF(width=465,height=465,backgroundColor=0xFFFFFF,frameRate=60)]
    
    public class Main110801_02 extends Sprite {
        public var sw:Number=stage.stageWidth;
        public var sh:Number=stage.stageHeight;
        public var _myLineAr:Array = new Array();
        private var myGravity:Number=0.1;
        private var myBounce:Number=-0.2;
        private var _myCircle:myCircle;
        private var _myLine:myLine;

        public function Main110801_02() {

            //ラインを配置
            for (var i:Number = 0; i<8; i++) {
                _myLine=new myLine() ;
                if (i%2==0) {
                    _myLine.rotation=-5;
                    _myLine.x=220;
                    _myLine.y=i*50+105;
                } else {
                    _myLine.rotation=5;
                    _myLine.x=0;
                    _myLine.y=i*50+75;
                }
                stage.addChild(_myLine);
                _myLineAr.push(_myLine);
            }

            //円を配置
            _myCircle=new myCircle  ;
            _myCircle.x=sw/2;
            _myCircle.y=100;
            addChild(_myCircle);

            addEventListener(Event.ENTER_FRAME,xEnter);

        }
        private function xEnter(e:Event):void {

            _myCircle.vy+=myGravity;
            _myCircle.x+=_myCircle.vx;
            _myCircle.y+=_myCircle.vy;

            if (_myCircle.y>sh) {
                _myCircle.y=0;
                //_myCircle.x=sw/2;
                _myCircle.vx=0;
                _myCircle.vy=0;

            }

            for (var ii:Number =0; ii<8; ii++) {
                myChk(_myLineAr[ii]);
            }
        }

        private function myChk(ActiveLine:Sprite):void {

            var myBound:Rectangle=ActiveLine.getBounds(this);

            if (_myCircle.x>myBound.left&&_myCircle.x<myBound.right) {

                var angle:Number=ActiveLine.rotation*Math.PI/180;
                var cos:Number=Math.cos(angle);
                var sin:Number=Math.sin(angle);

                var x1:Number=_myCircle.x-ActiveLine.x;
                var y1:Number=_myCircle.y-ActiveLine.y;
                var y2:Number=cos*y1-sin*x1;

                var vy1:Number=cos*_myCircle.vy-sin*_myCircle.vx;
                if (y2>- _myCircle.height/2 && y2<vy1) {
                    var x2:Number=cos*x1+sin*y1;

                    var vx1:Number=cos*_myCircle.vx+sin*_myCircle.vy;
                    

                    y2=- _myCircle.height/2;
                    vy1*=myBounce;

                    x1=cos*x2-sin*y2;
                    y1=cos*y2+sin*x2;
                    _myCircle.vx=cos*vx1-sin*vy1;
                    _myCircle.vy=cos*vy1+sin*vx1;
                    _myCircle.x=ActiveLine.x+x1;
                    _myCircle.y=ActiveLine.y+y1;
                }
            }
        }
    }

}

import flash.display.Sprite;

class myCircle extends Sprite {
    public var radius:Number=10;
    public var speed:Number=1;
    public var r:Number;
    public var vx:Number=0;
    public var vy:Number=0;

    public function myCircle() {
        graphics.beginFill(0xCCCCCC,1);
        graphics.drawCircle(0,0,radius);
        graphics.endFill();
    }
}


class myLine extends Sprite {

    public function myLine() {
        graphics.lineStyle(1,0x1F1F1F,0.5);
        graphics.moveTo(0,0);
        graphics.lineTo(250,0);
    }

}