[Actionscript3] 纯文本查看 复制代码
package
{
    import com.bit101.components.Panel;
    import com.bit101.components.RadioButton;
    import com.bit101.components.Window;
    import Box2D.Common.Math.b2Vec2;
    import Box2D.Dynamics.b2Body;
    import Box2D.Dynamics.b2World;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    /**
     * [url]http://www.ladeng6666.com[/url]
     * @author ladeng6666
     */
    public class Main extends Sprite
    {
        //创建世界的基本元素
        private var world:b2World;
        private var debugSprite:Sprite;
        private var body:b2Body;
        private var vector:b2Vec2 = new b2Vec2();
        private var currentMethod:String = "ApplyForce";
        public function Main()
        {
            world=LDEasyBox2D.createWorld();
            debugSprite=LDEasyBox2D.createDebug(world);
            addChild(debugSprite);
            setWrapWalls();
            //创建矩形刚体
            body = LDEasyBox2D.createBox(world, stage.stageWidth / 2, 0, 30, 30);
            //侦听事件
            addEventListener(Event.ENTER_FRAME, loop);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onStageKeyUp);
            setUI();
        }
        private function setWrapWalls():void
        {
            //创建左边的墙
            LDEasyBox2D.createBox(world, 0, stage.stageHeight / 2, 10, stage.stageHeight, true);
            //创建右边的墙
            LDEasyBox2D.createBox(world, stage.stageWidth, stage.stageHeight / 2, 10, stage.stageHeight, true);
            //创建地面,
            LDEasyBox2D.createBox(world,stage.stageWidth/2,stage.stageHeight,stage.stageWidth,10,true);
        }
        private function onStageKeyUp(e:KeyboardEvent):void
        {
            //清除速度或力
            vector.SetZero();
        }
        private function onStageKeyDown(ke:KeyboardEvent):void
        {
            switch (ke.keyCode) {
                case Keyboard.LEFT:
                    vector.x = -15;
                    break;
                case Keyboard.RIGHT:
                    vector.x = 15;
                    break;
            }
            trace(currentMethod);
            switch (currentMethod) {
                case "ApplyForce":
                    //在刚体上施加vector压力。
                    //body.GetWorldCenter()方法用来获取刚体的重心
                    body.ApplyForce(vector, body.GetWorldCenter());
                    break;
                case "ApplyImpulse":
                    //为刚体添加速度
                    body.ApplyImpulse(vector, body.GetWorldCenter());
                    break;
                case "SetLinearVelocity":
                    //唤醒刚体
                    body.WakeUp();
                    //设置刚体的线性速度
                    body.SetLinearVelocity(vector);
                    break;
            }            
        }
        private function loop(e:Event):void
        {
            world.Step(1 / 30, 10);
        }
        //下面的方法用来创建UI
        private function setUI():void
        {
            var panel:Panel = new Panel(stage, 400, 10);
            panel.width = 100;
            panel.height = 60;
            var radio1:RadioButton = new RadioButton(panel, 5, 5, "ApplyForce", true,onRadioChange);
            var radio2:RadioButton = new RadioButton(panel, 5, 25, "ApplyImpulse", false,onRadioChange);
            var radio3:RadioButton = new RadioButton(panel, 5, 45, "SetLinearVelocity", false,onRadioChange);
        }
        //侦听UI中的radio按钮变更事件
        private function onRadioChange(e:Event):void {
            currentMethod = e.target.label;
        }
    }
}