首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >保持球速box2d

保持球速box2d
EN

Stack Overflow用户
提问于 2012-08-20 15:22:30
回答 1查看 262关注 0票数 0

我正在使用这段代码来创建和投球。我想要增加球的速度,它应该保持这个速度直到它破坏。我应该怎么做才能得到结果??

代码语言:javascript
运行
复制
            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));

有谁可以帮我?

EN

回答 1

Stack Overflow用户

发布于 2012-08-20 16:02:06

为球提供一个Speed变量,并使用如下所示的函数:

代码语言:javascript
运行
复制
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

调用run(ball_mc);你想让它移动的每一帧。

像维护速度一样维护速度变量。

Self就是那个球。你首先需要根据它应该移动的方向来设置它的旋转,但这是一个不同的主题,所以我将节省空间并省略这一部分。

我希望这能帮到你。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12033669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档