flash on 2010-12-15

by yama3
♥0 | Line 143 | Modified 2010-12-15 17:50:47 | MIT License
play

ActionScript3 source code

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

package {
    import Box2D.Collision.b2AABB;
    import Box2D.Collision.Shapes.b2PolygonDef;
    import Box2D.Common.Math.b2Vec2;
    import Box2D.Dynamics.b2Body;
    import Box2D.Dynamics.b2BodyDef;
    import Box2D.Dynamics.b2DebugDraw;
    import Box2D.Dynamics.b2World;
    import Box2D.Dynamics.Joints.b2MouseJoint;
    import Box2D.Dynamics.Joints.b2MouseJointDef;
    import Box2D.Dynamics.Joints.b2RevoluteJointDef;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    
    [SWF(backgroundColor=0, width=400, height=400, frameRate=40)]
    
    public class FlashTest extends Sprite {
        private var world:b2World;
        private var myAABB:b2AABB;
        private const ratio:int = 30;
        private var joint:b2RevoluteJointDef = new b2RevoluteJointDef();
        private var prevBody:b2Body;
        private var mouseJoint:b2MouseJoint;
        private var boxBody:b2Body;
        private var isHold:Boolean = false;
        private var lightHandle:b2PolygonDef;
        private var heavyHandle:b2PolygonDef;
        private var bodyToDestroyList:Vector.<b2Body>=new Vector.<b2Body>();
        private var timer:int = 0;
        
        public function FlashTest() {
            setupHeavyAndLightDef();
            setupWorld();
            setupDebugDraw();
            createNubchuck();
            showInstruction();
            addEventListener(Event.ENTER_FRAME, render);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);            
        }
        
        private function showInstruction():void{
            var txt:TextField = new TextField();
            txt.text = "Hold Mouse to use tight handling\nRelease mouse to use loose handling\nMove mouse to swing";
            txt.textColor = 0xfff000;
            txt.mouseEnabled = false;
            txt.width = 400;
            txt.wordWrap = true;
            addChild(txt);
        }
        
        private function setupHeavyAndLightDef():void {
            heavyHandle = new b2PolygonDef();
            heavyHandle.SetAsBox(20/ratio, 2.5/ratio);
            heavyHandle.density = 2000;
            heavyHandle.filter.groupIndex = -1;
            lightHandle = new b2PolygonDef();
            lightHandle.SetAsBox(20/ratio, 2.5/ratio);
            lightHandle.density = 10;
            lightHandle.filter.groupIndex = -1;
        }
        
        private function createObject():void {
            var shape:b2PolygonDef = new b2PolygonDef();
            shape.SetAsBox(Math.random()*15+5) /ratio,(Math.random()*15+5)/ratio);
            shape.fillter.categoryBits = 1;
            shape.density = Math.random()/3+.1;
            var bodyDef:b2BodyDef = new b2BodyDef();
            bodyDef.position.Set((Math.random()*400)/ratio, 0);
            var body:b2Body = world.CreateBody(bodyDef);
            body.CreateShape(shape);
            body.SetMassFromShapes();
            body.ToDestroyList.push(body);
        }
        private function downHandler(e:MouseEvent):void {
            boxBody.DestroyShape(boxBody.GetShapeList());
            boxBody.CreateShape(heavyHandle);
            boxBody.SetMassFromShapes();
            isHold = true;
        }
        private function upHandler(e:MouseEvent):void {
            boxBody.DestroyShape(boxBody.GetShapeList());
            boxBody.CreateShape(heavyHandle);
            boxBody.SetMassFromShapes();
            isHold = true;
        }
        private function createNunchuck():void {
            var boxSD:b2PolygonDef = new b2PolygonDef();
            boxSD.SetAsBox(20/ratio, 2.5/ratio);
            boxSD.density = 10;
            boxSD.filter.groupIndex = -1;
            var boxBD:b2BodyDef = new b2BodyDef();
            boxBD.position.Set(mouseX/ratio, mouseY/ratio);
            boxBody = world.CreateBody(boxBD);
            boxBody.CreateShape(lightHandle);
            boxBody.SetMassShape(lightHandle);
            boxBody.SetMassFromShapes(boxSD);
            prevBody = boxBody;
            
            var boxBD2:b2BodyDef = new b2BodyDef();
            boxBD2.position.Set((mouseX+75) / ratio, mouseY / ratio);
            var boxBody2:b2Body = world.CreateBody(boxBD2);
            boxBody2.SetBullet(true);
            boxBody2.CreateShape(boxSD);
            boxBody2.SetMassFromShapes();
            
            var chainBodyShape:b2PolygonDef = new b2PolygonDef();
            chainBodyShape.SetAsBox(2.5 / ratio, .25 / ratio);
            
            chainBodyShape.fillter.groupIndex = -1;
            chainBodyShape.fillter.categoryBits = 1;
            for(var i:int = 0; i < 7; i++) {
                var chainSD:b2BodyDef = new b2BodyDef();
                chainBodyShape.density = 1000-i*50;
                chainSD.position.Set((mouseX+22.5 + i * 5/ratio, mouseY/ratio);
                var chainBody:b2Body = world.CreateBody(chainSD);
                chainBody.CreateShape(chainBodyShape);
                chainBody.SetMassFromShapes();
                joint.Initialize(prevBody, chainBody, new b2Vec2(((mouseX+22.5 + i * 5) - 2.5 / ratio, mouseY/ratio));
                world.CreateJoint(joint);
                prevBody = chainBody;
            }
            joint.Initialize(prevBody, boxBody2, new b2Vec2(((mouseX+22.5 + 7 * 5)-2.5)/ratio, mouseY/ratio));
            world.CreateJoint(joint);
            var mouseJointDef:b2MouseJointDef = new b2MouseJointDef();
            mouseJointDef.body2 = boxBodt;
            mouseJointDef.body1 = world.GetGroundBody();
            mouseJointDef.target.Set(mouseX/ ratio,mouseY/ratio);
            mouseJointDef.maxForce = 10000000;
            mouseJointDef.itmeStep = 1 / 80;
            mouseJoint = world.CreateJoint(mouseJointDef) as b2MouseJoint;
        }
        
        private function render(e:Event):void
        {
            if(isHold) {
                boxBody.SetAngularVelocity(0);
            }
            timer++;
            if(timer % 10 == 0) {
                createObject();
            }
            world.Step(1/40,20);
            mouseJoint.SetTarget(new b2Vec2(mouseX/ ratio, mouseY/ratio))
            for(var i:int=0; i<bodyToDestroyList.length; i++) {
                var myBody:b2Body = bodyToDestroyList[i];
                var body_y
            }



        }







    }
}