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

flash on 2011-11-14

Copyright aiyco ( http://wonderfl.net/user/aiyco )
MIT License ( http://www.opensource.org/licenses/mit-license.php )
Downloaded from: http://wonderfl.net/c/mX2o
Get Adobe Flash player
by naoyuki 14 Nov 2011
    Embed
/**
 * Copyright naoyuki ( http://wonderfl.net/user/naoyuki )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/94MO
 */

/**
 * Copyright aiyco ( http://wonderfl.net/user/aiyco )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mX2o
 */
 
package {
    import Box2D.Dynamics.*;
    import Box2D.Collision.*;
    import Box2D.Collision.Shapes.*;
    import Box2D.Common.Math.*;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.text.TextField;
 
    [SWF(backgroundColor="#ffffcc", width="350", height="200")]
    public class FlashTest extends Sprite {
        // 重力
        private const SCALE:Number = 8;
        private var world:b2World;
 
        private var tf:TextField;
        private var animation:Boolean = false;
        private var count:int = 0;
 
        // 世界
        private var worldAABB:b2AABB;
        private var gravity:b2Vec2;
        private var wallBdDef:b2BodyDef;
        private var wallBd:b2Body;
        private var wallShapeDef:b2PolygonDef;
        private var debugDraw:b2DebugDraw;
 
        // Object
        private var objBdDef:b2BodyDef;
        private var objBd:b2Body;
        private var shapeDef:b2PolygonDef;
 
        public function FlashTest() {
            // write as3 code here..
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
 
            tf = new TextField();
            tf.textColor = 0x000000;
            tf.text = "Click to start";
            addChild(tf);
            animation = false;
            stage.addEventListener(MouseEvent.CLICK, tfClickEventHandler);
 
            // 初期化
            init();
            createObject();
 
            count = 0;
            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
 
        /**
         * init
         */
        private function init():void {
            // 世界を作る
            worldAABB = new b2AABB();
            worldAABB.lowerBound.Set(-100.0, -100.0);
            worldAABB.upperBound.Set(100.0, 100.0);
 
            // 重力
            gravity = new b2Vec2(0.0, 10.0);
 
            // 世界のインスタンス
            world = new b2World(worldAABB, gravity, true);
 
            // 床
            wallBdDef = new b2BodyDef();
            wallBdDef.position.Set(400 / SCALE / 2, 300 / SCALE);
            wallBdDef.angle = Math.PI / 20;
 
            wallBd = world.CreateBody(wallBdDef);
 
            wallShapeDef = new b2PolygonDef();
            wallShapeDef.SetAsBox(180 / SCALE, 10 / SCALE);
 
            wallBd.CreateShape(wallShapeDef);
 
            // DebugDraw
            debugDraw = new b2DebugDraw();
            debugDraw.m_sprite = this;
            debugDraw.m_drawScale = SCALE;
            debugDraw.m_fillAlpha = .5;
            debugDraw.m_lineThickness = 3;
            debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
            world.SetDebugDraw(debugDraw);
        }
 
        /**
         * createObject
         */
        private function createObject():void {
            objBdDef = new b2BodyDef();
            objBdDef.position.Set((300 * Math.random()) / SCALE, 0);
            objBdDef.angle = Math.PI / 2 * Math.random();
 
            objBd = world.CreateBody(objBdDef);
 
            shapeDef = new b2PolygonDef();
            shapeDef.SetAsBox(30 / SCALE, 30 / SCALE);
            shapeDef.density = 1;
            shapeDef.restitution = 0.4;
            shapeDef.friction = 0.1;
 
            objBd.CreateShape(shapeDef);
 
            shapeDef.SetAsBox(40 / SCALE, 5 / SCALE);
            objBd.CreateShape(shapeDef);
 
            objBd.SetMassFromShapes();
        }        
 
        /**
         * tfClickEventHandler
         * @event : MouseEvent
         */
        private function tfClickEventHandler(event:MouseEvent):void {
            count = 0;
            animation = !animation;
            if (animation) {
                tf.text = "Click to stop";
            } else {
                tf.text = "Click to start";
            }
        }
 
        /**
         * enterFrameHandler
         * @event : Event
         */
        private function enterFrameHandler(event:Event):void {
            world.Step(1 / 15, 10);
            if (count == 0 && animation) {
                createObject();
            }
            count = (count + 1) % 30;
 
            // 下に行ったオブジェクトを削除
            for (var b:b2Body = world.GetBodyList(); b; b = b.GetNext()) {
                if (b.GetWorldCenter().y * SCALE > 600) {
                    world.DestroyBody(b);
                }
            }
        }
    }
}