forked from: flash on 2013-4-27

by alexnotkin forked from flash on 2013-4-27 (diff: 14)
source code from NUTSU.com a great master of code
♥0 | Line 140 | Modified 2013-04-27 05:57:10 | MIT License
play

ActionScript3 source code

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

// forked from alexnotkin's flash on 2013-4-27
 //source code from NUTSU.com a great master of code
         package 

{

    import Box2D.Collision.*;

    import Box2D.Collision.Shapes.*;

    import Box2D.Common.Math.*;

    import Box2D.Dynamics.*;

    import flash.display.*;

    import flash.events.*;

    import flash.geom.*;



    public class TestWorld extends MovieClip

    {

        private var WORLD_SCALE:Number = 100;

        private var VIEW_RECT:Rectangle;

        private var world:b2World;

        private var half_height:Number;

        private var half_width:Number;

        private var circles:Array;



        public function TestWorld()

        {
var centerX:Number = stage.stageWidth / 2;
var centerY:Number = stage.stageHeight / 2;
var back:Sprite = new Sprite();
back.graphics.beginFill(0x000000);
back.graphics.drawRect(0, 0, 2*centerX, 2*centerY);
back.graphics.endFill();
//addChild (back);


            WORLD_SCALE = 20;

            VIEW_RECT = new Rectangle(0, 0, stage.stageWidth / WORLD_SCALE, stage.stageHeight / WORLD_SCALE);

            half_width = stage.stageWidth * 0.5;

            half_height = stage.stageHeight * 0.5;

            circles = [];

            initWorld();

            initObjects();

            initDraw();

            addCircle(VIEW_RECT.width * 0.5, VIEW_RECT.height * 0.5);

            addEventListener("enterFrame", onEnterFrame);

            stage.addEventListener("enterFrame", onClick);

            stage.quality = "low";

            return;

        }// end function



        private function initObjects() : void

        {

            var _loc_1:* = NaN;

            var _loc_2:* = null;

            var _loc_3:* = null;

            var _loc_4:* = null;

            var _loc_5:* = null;

            var _loc_6:* = null;

            var _loc_7:* = null;

            _loc_1 = 0.025;

            _loc_2 = new b2PolygonDef();

            _loc_2.SetAsBox(_loc_1, VIEW_RECT.height * 0.5);

            _loc_2.density = 0;

            _loc_3 = new b2PolygonDef();

            _loc_3.SetAsBox(VIEW_RECT.width * 0.5, _loc_1);

            _loc_3.density = 0;

            _loc_4 = new b2BodyDef();

            _loc_5 = new b2BodyDef();

            _loc_6 = new b2BodyDef();

            _loc_7 = new b2BodyDef();

            _loc_4.position.Set(_loc_1, VIEW_RECT.height * 0.5);

            _loc_5.position.Set(VIEW_RECT.width - _loc_1, VIEW_RECT.height * 0.5);

            _loc_6.position.Set(VIEW_RECT.width * 0.5, VIEW_RECT.height - _loc_1);

            _loc_7.position.Set(VIEW_RECT.width * 0.5, _loc_1);

            world.CreateBody(_loc_4).CreateShape(_loc_2);

            world.CreateBody(_loc_5).CreateShape(_loc_2);

            world.CreateBody(_loc_6).CreateShape(_loc_3);

            world.CreateBody(_loc_7).CreateShape(_loc_3);

            return;

        }// end function



        private function initWorld() : void

        {

            var _loc_1:* = null;

            var _loc_2:* = null;

            var _loc_3:* = false;

            _loc_1 = new b2AABB();

            _loc_1.lowerBound.Set(-1000, -1000);

            _loc_1.upperBound.Set(1000, 1000);

            _loc_2 = new b2Vec2(0, 0);

            _loc_3 = false;

            world = new b2World(_loc_1, _loc_2, _loc_3);

            return;

        }// end function



        private function initDraw() : void

        {

            var _loc_1:* = null;

            _loc_1 = new b2DebugDraw();

            _loc_1.m_sprite = this;

            _loc_1.m_drawScale = WORLD_SCALE;

            _loc_1.m_fillAlpha = 1;

            _loc_1.m_lineThickness = 0;

            _loc_1.m_drawFlags = b2DebugDraw.e_shapeBit;

            world.SetDebugDraw(_loc_1);

            return;

        }// end function



        private function onEnterFrame(event:Event) : void

        {

            var _loc_2:* = 0;

            var _loc_3:* = null;

            if (world)

            {

                world.m_gravity.x = -2 * (half_width - mouseX) / half_width;

                world.m_gravity.y = -2 * (half_height - mouseY) / half_height;

                _loc_2 = 0;

                while (_loc_2 < circles.length)

                {

                    

                    _loc_3 = circles[_loc_2];

                    _loc_3.m_radius = 0.2 + Math.sin(_loc_3.m_userData) * 0.1;

                    _loc_3.m_userData = _loc_3.m_userData + 0.05;

                    _loc_2++;

                }

                world.Step(1 / 15, 4);

            }

            return;

        }// end function



        private function addCircle(param1:Number, param2:Number) : void

        {

            var _loc_3:* = null;

            var _loc_4:* = null;

            var _loc_5:* = null;

            var _loc_6:* = null;

            _loc_3 = new b2CircleDef();

            _loc_3.density = 1;

            _loc_3.restitution = 0.2;

            _loc_3.radius = 0.2;

            _loc_4 = new b2BodyDef();

            _loc_4.position.Set(param1, param2);

            _loc_5 = world.CreateBody(_loc_4);

            _loc_6 = _loc_5.CreateShape(_loc_3);

            _loc_5.SetMassFromShapes();

            _loc_6.m_userData = Number(0);

            circles.push(b2CircleShape(_loc_6));

            return;

        }// end function



        private function onClick(event:Event) : void

        {

            addCircle(mouseX / WORLD_SCALE, mouseY / WORLD_SCALE);

            return;

        }// end function



    }

}