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: forked from: 【box2d】重力ドットデジタル時計

// forked from hacker_ciaxk5up's forked from: 【box2d】重力ドットデジタル時計


package {
    //必要なクラスのインポート
    import Box2D.Collision.*;
    import Box2D.Common.Math.*;
    import Box2D.Dynamics.*;
    import Box2D.Collision.Shapes.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.filters.*;
    import flash.geom.*;
    import caurina.transitions.Tweener;
    import caurina.transitions.properties.*;
    public class Clock extends Sprite {
        
        private const SCALE:Number = 10;//box2d系座標定数 10ピクセルで1メートル
        private var world:b2World;//Box2d系世界を表す変数world
        private var nameStr:String = "abcdefghijklmnopqrstuvwxyz";//ドット1つ1つに付ける名前
        private var cAr:Array = [];//ドットを表す円を格納する配列
        private var movedCcl:int = 0;
        private var bd:BitmapData;
        private var now:Date;
        private var ss:String;
        private var mm:String;
        private var hh:String;
        private var tf:TextField;
        private var ctf:TextField;
        private var ccl:b2CircleDef;
        
        public function Clock() {
            //FilterShortcuts.init();
            ColorShortcuts.init();  
            init();
            }
        public function init():void
        {    
            stage.align = "TL";
            stage.scaleMode = "noScale";
            
            now = new Date();
            ss = now.seconds < 10? "0"+String(now.seconds):String(now.seconds);
            mm = now.minutes < 10? "0"+String(now.minutes):String(now.minutes);
            hh = now.hours < 10? "0"+String(now.hours):String(now.hours);
            
            var worldAABB:b2AABB = new b2AABB();
            worldAABB.lowerBound.Set(-100.0, -100.0);
            worldAABB.upperBound.Set(100.0, 100.0);
            var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
            world = new b2World(worldAABB, gravity, true);
            
            var wallBdDef:b2BodyDef = new b2BodyDef();
            wallBdDef.position.Set(stage.stageWidth / SCALE/2, (stage.stageHeight+20) / SCALE);
            var wallBd:b2Body = world.CreateBody(wallBdDef);
            var wallShapeDef:b2PolygonDef = new b2PolygonDef();
            wallShapeDef.SetAsBox((stage.stageWidth + 50) / SCALE, 20 / SCALE);
            wallBd.CreateShape(wallShapeDef);
            
            ccl = new b2CircleDef();
            ccl.radius = 6 / SCALE;
            ccl.density = 0.1;
            ccl.friction = 0;
            ccl.restitution = 0.7;
            
            tf = new TextField();
            var format:TextFormat = new TextFormat();
            //format.font = "Times New Roman";
            //tf.defaultTextFormat = format;
            ctf = TextField(addChild(new TextField()));
            ctf.autoSize = "left";
            ctf.text = tf.defaultTextFormat.font
            tf.textColor = 0xff9933;
            tf.text = String(hh)+":"+String(mm)+":"+String(ss);
            tf.autoSize = "left";
            
            bd = new BitmapData(tf.width, tf.height, false, 0xffffff);
            bd.draw(tf);
            
            for(var i:int = 0; i < bd.width; i++){
                for(var j:int = 0; j < bd.height; j++){
                    if(bd.getPixel(i, j)<0xffdfff){
                    var realx:int;
                    var realy:int;
                    var circle:Sprite = new Sprite();
                    circle.graphics.beginFill(bd.getPixel(i, j));
                    circle.graphics.drawCircle(0, 0, 6);
                    circle.graphics.endFill();
                    realx = i * 10;
                    realy = j * 10;
                    circle.x = realx + Math.random() * 300 - 150;
                    circle.y = realy + Math.random() * 300 - 150;
                    circle.alpha = 0;
                    circle.name = nameStr.charAt(int(Math.random() * nameStr.length));
                    cAr.push(circle);
                    addChild(circle);
                    Tweener.addTween(circle, {x:realx, y:realy, alpha: 1, time: 1,
                            delay: Math.sqrt(i + j) * Math.random(), onComplete:function():void{movedCcl++}});
                                    }
                    }
            }
            stage.addEventListener(Event.ENTER_FRAME,checkMoved);
        }
       
        private function checkMoved(e:Event):void
        {
            if(movedCcl>=cAr.length)
            {
                stage.addEventListener(Event.ENTER_FRAME,clockStart);
                stage.removeEventListener(Event.ENTER_FRAME,checkMoved);
                movedCcl = 0;
                now = new Date();
                ss = now.seconds.toString();
            }
        }
        
        private function clockStart(e:Event):void
        {
            now = new Date();
            
            for (var b:b2Body = world.GetBodyList(); b; b = b.GetNext())
            {
                if (b.GetWorldCenter().x * SCALE > 600 || b.GetWorldCenter().x * SCALE < -20)
                {
                    if (b.GetUserData() is Sprite)
                    {
                        removeChild(b.GetUserData());
                    }
                    world.DestroyBody(b);
                }
                if (b.GetUserData() is Sprite)
                {
                    b.GetUserData().x = b.GetWorldCenter().x * SCALE;
                    b.GetUserData().y = b.GetWorldCenter().y * SCALE;
                } 
            }
            world.Step(1 / 15, 5);
            
            if(now.seconds != int(ss))
            {
                cAr.sortOn("name");
                movedCcl = 0;
                ss = now.seconds < 10? "0"+String(now.seconds):String(now.seconds);
                mm = now.minutes < 10? "0"+String(now.minutes):String(now.minutes);
                hh = now.hours < 10? "0"+String(now.hours):String(now.hours);
                
                tf.text = String(hh)+":"+String(mm)+":"+String(ss);
                tf.autoSize = "left";
                ctf.text = String(hh)+":"+String(mm)+":"+String(ss);
                bd = new BitmapData(tf.width, tf.height, false, 0xffffff);
                bd.draw(tf);
                
                for(var k:int = 0; k < bd.width; k++)
                {
                    for(var j:int = 0; j < bd.height; j++)
                    { 
                        if(bd.getPixel(k, j)<0xffdfff)
                        {
                            var realx:int;
                            var realy:int;
                            if(cAr.length<=movedCcl){
                                var circle:Sprite = new Sprite();
                                circle.graphics.beginFill(bd.getPixel(k, j));
                                circle.graphics.drawCircle(0, 0, 6);
                                circle.graphics.endFill();
                                realx = k * 10;
                                realy = j * 10;
                                circle.x = realx + Math.random() * 300 - 150;
                                circle.y = realy + Math.random() * 300 - 150;
                                circle.alpha = 0;
                                circle.name = nameStr.charAt(int(Math.random() * nameStr.length));
                                cAr.push(circle);
                                addChild(circle);
                                }
                                
                            if(cAr.length>movedCcl){
                            
                            realx = k * 10;
                            realy = j * 10;
                            cAr[movedCcl].name = nameStr.charAt(int(Math.random() * nameStr.length));
                            Tweener.addTween(cAr[movedCcl], {x:realx, y:realy, time:0.5, _color:bd.getPixel(k, j), transition:"linear", alpha:1});}
                            movedCcl++;
                        }
                    }
                }
                
                if(movedCcl<cAr.length)
                {
                    var rmCcl:int = cAr.length - movedCcl;
                    for(var re:int =0;re<rmCcl;re++)
                    {
                        var bod:b2BodyDef;
                        bod = new b2BodyDef();
                        bod.position.Set(cAr[movedCcl + re].x / SCALE, cAr[movedCcl + re].y / SCALE);
                        bod.userData = cAr[movedCcl + re];
                        var circbd:b2Body = world.CreateBody(bod);
                        circbd.CreateShape(ccl);
                        circbd.SetMassFromShapes();
                        
                    }
                    cAr.splice(movedCcl,rmCcl);
                }
            }
        }
    }
}