在AS3,Box2d中,资产的移动速度比其父b2Body快

时间:2014-03-08 11:14:47

标签: actionscript-3 box2d flash-cs5

我正在努力创造一个只能在斜坡上冲浪的滑雪板。但是当我运行项目时,我添加的资产运行速度比它的父体更快。它刚刚用完了b2Body的界限。为什么即使在相对于它的父b2body设置其位置之后也会发生这种情况。这是我使用的代码。更不用说我是Box2d的新手了。

 package  {

        import flash.display.MovieClip;
        import flash.display.Sprite;
        import Box2D.Collision.*;
        import Box2D.Common.*;
        import Box2D.Dynamics.*;
        import Box2D.Dynamics.Joints.*;
        import Box2D.Dynamics.Contacts.*;
        import Box2D.Common.Math.*;
        import Box2D.Collision.Shapes.*;
        import flash.events.Event;
        import flash.events.KeyboardEvent;
        import flash.sampler.NewObjectSample;
        import flash.display.Shape;

        public class PhysSnowboarder extends Sprite {
            //Degree to Radian Constant
            private const degreesToRadians:Number=0.0174532925;

            //World Definition
            public var worldScale:Number=30;
            private var world:b2World= new b2World(new b2Vec2(0,9.8),true);

            //Key Flags (Game Control Keys)
            private var left:Boolean=false;
            private var right:Boolean=false;

            //Hill attributes and misc
            private var nextHill:Number=240;
            private var realHeight:Number=240;
            private var realWidth:Number=0;
            private var buildNextHillAt:Number=0;


            //Sphere
            var theSphere:b2Body;
            var riderBody:b2Body;
            //Game Canvas
            private var gameCanvas:Sprite= new Sprite();

            public function PhysSnowboarder(){
                // i created a simple image of a snowboarder, created an as linkage. 
                        // Inherits flash.display.sprite

                var boardRider:BoardRider= new BoardRider();

                //create a sphere
                var sphereShape:b2CircleShape= new b2CircleShape(50/worldScale);
                var sphereFixture:b2FixtureDef= new b2FixtureDef();
                sphereFixture.density=2;
                sphereFixture.friction=3;
                sphereFixture.restitution=0.1;
                //sphereFixture.filter.groupIndex=-1;
                sphereFixture.shape=sphereShape;


                //sphere body 
                var sphereBodyDef:b2BodyDef= new b2BodyDef();
                sphereBodyDef.type=b2Body.b2_dynamicBody;
                sphereBodyDef.position.Set(320/worldScale,0);
                sphereBodyDef.userData=new Object();
                sphereBodyDef.userData.asset=boardRider;

                //Add the sphere to stage
                theSphere=world.CreateBody(sphereBodyDef);
                theSphere.SetUserData(sphereBodyDef.userData);
                theSphere.CreateFixture(sphereFixture);
                addChild(sphereBodyDef.userData.asset);
                //theSphere.SetUserData(boardRider);
                //addChild(theSphere.GetUserData());

                addChild(gameCanvas);
                //stage.addChild(new ());

                debugDraw();
                while(realWidth<1280){
                    nextHill=drawHill(5,realWidth,nextHill);

                }
                addEventListener(Event.ENTER_FRAME,updateWorld);
                stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
                stage.addEventListener(KeyboardEvent.KEY_UP,keyReleased);

            }

            //debugDraw
            private function debugDraw():void{
                var worldDebugDraw:b2DebugDraw= new b2DebugDraw();
                var debugSprite:Sprite=  new Sprite();
                gameCanvas.addChild(debugSprite);

                worldDebugDraw.SetSprite(debugSprite);
                worldDebugDraw.SetDrawScale(worldScale);
                worldDebugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);
                worldDebugDraw.SetFillAlpha(0.5);
                world.SetDebugDraw(worldDebugDraw);
            }

            //drawHill 

            private function drawHill(pixelStep:int,xOffset:Number,yOffset:Number):Number{
                var hillStartY:Number=yOffset;
                var hillWidth:Number=120+Math.ceil(Math.random()*26)*20;
                realWidth+=hillWidth;

                var numberOfSlices=hillWidth/pixelStep;
                var hillVector:Vector.<b2Vec2>;
                var randomHeight:Number;

                if(xOffset==0){
                    randomHeight=0;
                }
                else{
                    do{
                        //randomHeight=Math.random()*hillWidth/7.5;
                        randomHeight=240/worldScale;
                    }while(realHeight+randomHeight>600);
                }

                realHeight+=randomHeight;
                hillStartY-=randomHeight;

                for (var j:int=0; j<numberOfSlices/2; j++) {
                    hillVector=new Vector.<b2Vec2>();
                    hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,480/worldScale));
                    hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*j))/worldScale));
                    hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*(j+1)))/worldScale));
                    hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,480/worldScale));
                    var sliceBody:b2BodyDef=new b2BodyDef  ;
                    var centre:b2Vec2=findCentroid(hillVector,hillVector.length);
                    sliceBody.position.Set(centre.x,centre.y);
                    for (var z:int=0; z<hillVector.length; z++) {
                        hillVector[z].Subtract(centre);
                    }
                    var slicePoly:b2PolygonShape=new b2PolygonShape  ;
                    slicePoly.SetAsVector(hillVector,4);
                    var sliceFixture:b2FixtureDef=new b2FixtureDef  ;
                    sliceFixture.shape=slicePoly;
                    var worldSlice:b2Body=world.CreateBody(sliceBody);
                    worldSlice.CreateFixture(sliceFixture);
                }
                hillStartY-=randomHeight;



                if (xOffset==0) {
                    randomHeight=0;
                }
                else {
                    do {
                        randomHeight=Math.random()*hillWidth/5;
                    } while (realHeight-randomHeight<240);
                }
                realHeight-=randomHeight;
                hillStartY+=randomHeight;

                for (j=numberOfSlices/2; j<numberOfSlices; j++) {
                    hillVector=new Vector.<b2Vec2>();
                    hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,480/worldScale));
                    hillVector.push(new b2Vec2((j*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*j))/worldScale));
                    hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/numberOfSlices*(j+1)))/worldScale));
                    hillVector.push(new b2Vec2(((j+1)*pixelStep+xOffset)/worldScale,480/worldScale));
                    sliceBody=new b2BodyDef  ;
                    centre=findCentroid(hillVector,hillVector.length);
                    sliceBody.position.Set(centre.x,centre.y);
                    for (z=0; z<hillVector.length; z++) {
                        hillVector[z].Subtract(centre);
                    }
                    slicePoly=new b2PolygonShape  ;
                    slicePoly.SetAsVector(hillVector,4);
                    sliceFixture=new b2FixtureDef  ;
                    sliceFixture.shape=slicePoly;
                    worldSlice=world.CreateBody(sliceBody);
                    worldSlice.CreateFixture(sliceFixture);
                }
                hillStartY=hillStartY+randomHeight;
                return (hillStartY);
            }

            //findCentroid
            private function findCentroid(vs:Vector.<b2Vec2>,count:uint):b2Vec2{
                var c:b2Vec2= new b2Vec2();
                var area:Number=0.0;
                var p1X:Number=0.0;
                var p1Y:Number=0.0;

                var inv3:Number=1.0/3.0;

                for(var i:int=0; i<count;++i){
                    var p2:b2Vec2=vs[i];
                    var p3:b2Vec2=i+1<count?vs[int(i+1)]:vs[0];

                    var e1X:Number=p2.x-p1X;
                    var e1Y:Number=p2.y-p1Y;

                    var e2X:Number=p3.x-p1X;
                    var e2Y:Number=p3.y-p1Y;

                    var D:Number=(e1X*e2Y-e1Y*e2X);
                    var triangleArea:Number=0.5*D;

                    area+=triangleArea;
                    c.x+=triangleArea*inv3*(p1X+p2.x+p3.x);
                    c.y+=triangleArea*inv3*(p1Y+p2.y+p3.y);

                    }
                    c.x*=1.0/area;
                    c.y*=1.0/area;

                    return c;

            }

            //updateWorld

            private function updateWorld(e:Event):void{


                if(left)
                {
                    //theSphere.SetLinearVelocity(new b2Vec2(-10,0));
                    theSphere.ApplyTorque(-3);
                }
                if(right)
                {
                    //theSphere.SetLinearVelocity(new b2Vec2(10,0));
                    theSphere.ApplyTorque(3);

                    //theSphere.ApplyTorque(0.5);

                }


                world.Step(1/30,10,10);
                world.ClearForces();

                for(var currentBody:b2Body=world.GetBodyList(); currentBody; currentBody=currentBody.GetNext())
                {
                    if(currentBody.GetUserData()!=null){
                        gameCanvas.x=320-currentBody.GetPosition().x*worldScale;
                        gameCanvas.y=240-currentBody.GetPosition().y*worldScale;
                        //gameCanvas.addChild(currentBody.GetUserData());
                        if(gameCanvas.x<=-realWidth+800){
                            nextHill=drawHill(5,realWidth,nextHill);
                        }

//This is where i set the position of the of the asset i created.
                        currentBody.GetUserData().asset.x=(currentBody.GetPosition().x*worldScale);
                        currentBody.GetUserData().asset.y=((currentBody.GetPosition().y+(50/worldScale))*worldScale);


                    }

                    if(currentBody.GetPosition().x*worldScale<(gameCanvas.x*-1)-640){
                        world.DestroyBody(currentBody);
                    }
                }
                world.DrawDebugData();
            }

            //keyPressed
            private function keyPressed(e:KeyboardEvent):void {
                switch (e.keyCode) {
                    case 37 :
                        left=true;
                        break;
                    case 39 :
                        right=true;
                        break;
                }
            }
            //keyReleased
            private function keyReleased(e:KeyboardEvent):void {
                switch (e.keyCode) {
                    case 37 :
                        left=false;
                        break;
                    case 39 :
                        right=false;
                        break;
                }
            }



        }//Class Ends here

    }//Package Ends here

0 个答案:

没有答案