0
votes

I'm using this code to Create and throw the ball. I want to increase the speed of the ball and it should maintain the speed until it destroy. What should i do to getting the result??

            var sphereShape:b2CircleShape=new b2CircleShape(15/worldScale);
            var sphereFixture:b2FixtureDef = new b2FixtureDef();
            sphereFixture.density=1;
            sphereFixture.friction=3;
            sphereFixture.restitution=0.1;
            sphereFixture.filter.groupIndex = -1;
            sphereFixture.shape=sphereShape;
            ball_mc=new ballMc();
            ballCont_mc.addChild(ball_mc);
            var sphereBodyDef:b2BodyDef = new b2BodyDef();
            sphereBodyDef.type=b2Body.b2_dynamicBody;
            ball_mc.name="throwBall";
            ball_mc.hitCount=0;
            ball_mc.basketHit=0;
            sphereBodyDef.position.Set(arrow_mc.x/worldScale,arrow_mc.y/worldScale);
            birdSphere=world.CreateBody(sphereBodyDef);
            birdSphere.CreateFixture(sphereFixture);girl_mc.gotoAndStop(3);
            birdSphere.SetUserData(ball_mc);
            birdSphere.SetBullet(true);
            var distanceX:Number=arrow_mc.x-mouseX;
            var distanceY:Number=arrow_mc.y-mouseY;
            var distance:Number=Math.sqrt(distanceX*distanceX+distanceY*distanceY);
            var birdAngle:Number=Math.atan2(distanceY,distanceX);
            birdSphere.SetLinearVelocity(new b2Vec2(distance*Math.cos(birdAngle)/4,distance*Math.sin(birdAngle)/4));

Can anyone help me?

1

1 Answers

3
votes

Give the ball a Speed variable and use a function similar to the following:

function run(Self)
 {
   Self._x+=Math.sin(Self._rotation*(Math.PI/180))*Self.Speed/10;
   Self._y+=Math.cos(Self._rotation*(Math.PI/180))*Self.Speed/10*-1;
 }//------------------------------------------------------------^run

Call run(ball_mc); every frame you want it to move.

Maintain the Speed variable as you would a velocity.

Self is simply the ball. You will first need to set its rotation according to what direction it should be moving in, but that's a different topic so I'll save the space and leave that part out.

I hope this helps.