Box2DFlash ApplyForce()

时间:2014-03-10 03:37:41

标签: flash

我正在使用Box2dFlash开发一款新游戏。我写了一个简单的代码,当我点击舞台时,一个球会移动。我使用ApplyForce()函数来完成此任务。但问题是当球开始移动时它不会停止直到它进入舞台的一角。它不断移动到给定的速度。有什么方法可以在每次移动时降低球的速度?我的代码如下 -

        package
        {   
            import Box2D.Collision.Shapes.b2CircleShape;
            import Box2D.Collision.Shapes.b2PolygonShape;
            import Box2D.Common.Math.b2Vec2;
            import Box2D.Common.Math.b2Vec3;
            import Box2D.Dynamics.b2Body;
            import Box2D.Dynamics.b2BodyDef;
            import Box2D.Dynamics.b2DebugDraw;
            import Box2D.Dynamics.b2FixtureDef;
            import Box2D.Dynamics.b2World;
            import flash.display.MovieClip; 
            import flash.display.Sprite;
            import flash.events.Event;
            import flash.events.KeyboardEvent;
            import flash.events.MouseEvent;

            public class Main extends MovieClip
            {   
                private var world:b2World;
                private const scale:Number = 30;
                private var b1:b2Body;

                public function Main()
                {
                    if (stage) init();
                    else addEventListener(Event.ADDED_TO_STAGE, init);
                    trace("physics1...");
                }
                private function init(e:Event=null):void
                {
                    removeEventListener(Event.ADDED_TO_STAGE, init);
                    getStarted();
                }
                private function getStarted():void 
                {
                    createWorld();
                    debugDraw();
                    drawBox(stage.stageWidth / 2, stage.stageHeight - 5, stage.stageWidth, 10);
                    drawBox(stage.stageWidth / 2, 5, stage.stageWidth, 10);
                    drawBox(5, stage.stageHeight / 2, 10, stage.stageHeight);
                    drawBox(stage.stageWidth - 5, stage.stageHeight / 2, 10, stage.stageHeight);
                    b1 = drawBall(10, stage.stageWidth / 2, stage.stageHeight / 2);
                    b1.GetFixtureList().SetRestitution(0.4);
                    b1.GetFixtureList().SetFriction(1.0);
                    b1.GetFixtureList().SetDensity(20);
                    addEventListener(Event.ENTER_FRAME, update);
                    stage.addEventListener(MouseEvent.CLICK, onClick);
                }
                private function onClick(e:MouseEvent):void 
                {
                    b1.ApplyForce(new b2Vec2(200, 200), b1.GetWorldCenter());
                }
                private function update(e:Event):void 
                {
                    world.Step(1 / 30, 10, 10);
                    world.ClearForces();
                    world.DrawDebugData();
                }
                private function createWorld():void
                {
                    world = new b2World(new b2Vec2(0, 0), true);
                }
                private function debugDraw():void
                {
                    var dd:b2DebugDraw = new b2DebugDraw();
                    var sp:Sprite = new Sprite();
                    addChild(sp);
                    dd.SetSprite(sp);
                    dd.SetDrawScale(scale);
                    dd.SetFlags(b2DebugDraw.e_shapeBit);
                    world.SetDebugDraw(dd);
                }
                private function drawBall(r:Number, _x:Number, _y:Number):b2Body
                {
                    var bd:b2BodyDef = new b2BodyDef();
                    bd.position.Set(_x / scale, _y / scale);
                    bd.type = b2Body.b2_dynamicBody;
                    var fd:b2FixtureDef = new b2FixtureDef();
                    var ps:b2PolygonShape = new b2PolygonShape();
                    ps.SetAsBox(r / scale, r / scale);
                    fd.shape = new b2CircleShape(r / scale);
                    var b:b2Body = world.CreateBody(bd);
                    b.CreateFixture(fd);
                    return b;
                }
                private function drawBox(_x:Number,_y:Number,_w:Number,_h:Number):b2Body
                {
                    var bd:b2BodyDef = new b2BodyDef();
                    bd.position.Set(_x / scale, _y / scale);
                    var ps:b2PolygonShape = new b2PolygonShape();
                    ps.SetAsBox(_w / 2 / scale, _h / 2 / scale);
                    var fd:b2FixtureDef = new b2FixtureDef();
                    fd.shape = ps;
                    var b:b2Body = world.CreateBody(bd);
                    b.CreateFixture(fd);
                    return b;
                }
            }   
        }

1 个答案:

答案 0 :(得分:1)

您可以为身体添加线性阻尼:

b1.SetLinearDamping(3);