简体   繁体   English

LibGDX:移动然后立即停止box2d

[英]LibGDX: move and then stop box2d immediately

I'm working on a project in LibGDX. 我正在LibGDX中的一个项目上。 It's a 2d platform game. 这是一个2D平台游戏。 you can say it's like Super Mario. 你可以说这就像超级马里奥。

So this is how i move my player to the right: 所以这就是我将播放器向右移动的方式:

if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
    player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
}

When i leave the key, the player is still moving a bit. 当我离开钥匙时,播放器仍在移动。 (it's still have "Linear Impulse" force on his physical body i think). (我认为他的身体上仍然有“线性冲动”力)。

How can i make it stop? 我该如何停止?

My question could be simple as: What is the best way to move a physical body on LibGDX in one direction while holding a key. 我的问题可能很简单:在按住键的同时,在LibGDX上沿一个方向移动实体的最佳方法是什么? and when i leave the key, the player stops immediately. 当我离开钥匙时,播放器立即停止。

bdw - I have tryied with "setTransform" and it's making issues when the body is touching other objects - plus the sahpe come "before" the rest of the game in the screen (you can see it on debug) bdw-我尝试过“ setTransform”,并且当身体接触其他对象时会出现问题-而且sahpe出现在屏幕上其余游戏的“之前”(您可以在调试时看到它)

Thanks. 谢谢。

Your method is OK. 您的方法还可以。 I would also recommend to set the max speed of your body: 我还建议您设置身体的最大速度:

if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x < maxSpeed)
    player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
}

Stop your body immediately by using the setLinearVelocity method.: 通过使用setLinearVelocity方法立即停止身体。

player.b2body.setLinearVelocity(0f, 0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM