All with LibGdx in render

if (Gdx.input.justTouched()) { // прикоснулся к экрану while (bullet.y < 500) { bullet.x -= 100 * Gdx.graphics.getDeltaTime(); bullet.y += 100 * Gdx.graphics.getDeltaTime(); } } 

I want to make sure that when pressed, the bullet flew with constant acceleration somewhere. From while she instantly turns out to be 500 y. If you remove the conditions

 if (Gdx.input.justTouched()) { while (bullet.y < 500) { 

, the bullet flies constantly and as needed. If you delete only while , it shifts by a specified distance. I can not understand why because while she behaves.

    1 answer 1

    Games work in a life cycle. I don’t know how in libdhs, but in general there are usually methods like update that are called per frame. so if you call while for one frame in update or a similar method, then it will be fully executed in one frame and you will get an instantly displaced bullet in this case. Just write

     if (bullet.y < 500) 

    and everything should be fine. You will probably also need to rewrite the if condition (Gdx.input.justTouched ()) without checking it every frame. You can make any flag, for example isBulletGone, which shows that at the moment the bullet is flying, this flag should be set to true when pressed. The usage logic may differ depending on the one you need, but in any case you will need to manage the states of the objects.

     if (Gdx.input.justTouched()) { isBulletGone = true; } if (isBulletGone && bullet.y < 500) { bullet.x -= 100 * Gdx.graphics.getDeltaTime(); bullet.y += 100 * Gdx.graphics.getDeltaTime(); } else { isBulletGone = false; } 

    Perhaps it would be better to make bullet a participant in the life cycle, which simply flies while the condition is fulfilled, after which it is destroyed.