java - How to shoot bullets when space bar is pressed using LibGDX? -


i did follows:

... if (gdx.input.iskeypressed(keys.space)) {    shoot(); } ... 

the problem if keep pressing space many bullets created. want bullet shot when press space not while i'm pressing key.

took @ documentation library, , doesn't seem expose other way of getting key presses (particularly key down/release). in such case can keep track of keep changes spacealreadypressed variable persists between frames.

... boolean spaceispressed = gdx.input.iskeypressed(keys.space); if (spaceispressed && !spacealreadypressed) {    shoot(); } ... spacealreadypressed = spaceispressed; 

it may safer use spaceispressed variable in case input state changes unexpectedly.


alternatively, if want make shorter, can use logical laws reduce following, canshoot persists between frames , has initial value of false.

... canshoot = !canshoot && gdx.input.iskeypressed(keys.space); if (canshoot) {    shoot(); } ... 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -