shooting delays and movement issues

Good evening gamers!
I have 2 problems:
Problem 1:
when I hold press the space key to shoot, 2 things happen:
- at first it shoot one bullet and then it stops for about a second and then it continue shooting the other bullets
- while I’m shooting the bullets if I pressed the down or up keys to move the spaceship it stops shooting.

problem 2:
if I pressed the right arrow key to move the spaceship and then I quickly pressed the left arrow key the spaceship stops
for about a second and it continue moving to the left.

how can I solve these 2 problems ? what I want is this:

  1. I want the moment I press the space key to shoot, it start shooting immediately bullet after the other.
  2. I want to be able to shoot while I’m moving the ship.
  3. I want to be able to move smoothly right and left without any delays.

here’s my code:


public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_SPACE) {
            fire();
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 2;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -2;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 2;
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = -2;
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }
    }

And here’s a link to download the game in a jar extension + the source code :
https://www.mediafire.com/?ebc5okuk2uk0g05

The repeating of the key press is the same as what you get when typing. Try pressing a key in a text field, notepad or whatever and you’ll see.

It’s not clear if your using and engine or Java 2D and custom coding, I don’t want to get it all from MediaFire, could you put it on the the pastebin (http://pastebin.java-gaming.org/). I think you may need to use polling to get a constant input, at least this is what I have done, see this: http://pastebin.java-gaming.org/7fb059e290315

You can then use:

if (keyInput == KeyEvent.VK_SPACE) {
    fire();
}

In your game loop. You need to use:

KeyInput keyInput = KeyInput.getInstance();

And:

addKeyListener(keyInput);

To use this. If it’s of any use to you feel free to do as you wish with the code, it’s fairly generic. It should solve all the problems you’ve listed used properly.

Thanks daniel for replaying, here’s the code :
http://www.java-gaming.org/?action=pastebin&id=1030
files in the link goes like this: Window.java, Screen.java, SpaceShip.java, Bullet.java, and AlienShip.java

and I’m using java Graphics2D
I’m gonna go now and try what you wrote

I’m going to also suggest you have a look at Game loops!. Not that your approach won’t work just that it probably isn’t ‘best’.

I’m not sure what fragment in my code the loop is happening !
any idea ?

You don’t have a loop… you’ve a timer hence why I gave you that link. I’ve never used a timer for anything I’ve done but I think you’d use the code within:

public void actionPerformed(ActionEvent ae) {
    // Game logic and painting?
    if (keyInput == KeyEvent.VK_SPACE) {
        fire();
    }
}

Rather than do this though I’d suggest you read through the link and consider rewriting to use a loop, it should take too much to do.

after changing this code:


if(keyInput == KeyEvent.VK_SPACE){
            
}

to this(I believe this is what you meant ?)


if (keyInput.getKeyState(KeyEvent.VK_SPACE)) {
    fire();
}

your class KeyInput is working perfectly ! with all the problems are solved !

there’s just one more thing, when I use your class the bullets are way too fast ! it now looks like a laser ! :clue: instead of being separated bullets. I think this is related to the game loop you were talking about right ?

Is there any chance to slow the frames down while keeping my code the same using timers ?

The problem with timers is that, iirc, it doesn’t keep consistent timing, so even if you slow it down it may seem like it’s working correctly and then occasionally it will run slower (maybe faster as well?).

Anyway I’m pretty sure that when you call new Timer() you can change 5 to something larger and it will slow down the game, but I wouldn’t recommend this approach either.

Here’s another good article, but it may be over your head as it’s pretty in-depth.

Also, your logic “loop” is actually actionPerformed().

Actually I did tried to put a larger number than 5 but the issue is with the frames, there is too much frames are being rendered no matter how much I change the timer, the bullets should be separate but they’re connected instead.

EDIT:

I fixed it by doing this(Just temporary, I know this is not the professional way)


int delay = 0;
++delay;
while (delay > 60) {
    delay = 0;
     if (keyInput.getKeyState(KeyEvent.VK_SPACE)) {
         spaceShip.fire();
    }
}

Now I can see the bullets !
Note: never mind the variable ‘delay’ I know it doesn’t make sense!

That…hurts my soul. haha. It’s OK if it works it works.

What you might consider doing though is taking your game logic out of the draw loop, paint(), and putting that logic in your actionPerformed(). The reason being that when you change your timer interval it should slow down the gameplay, the bullets shooting, ships moving, etc, but it won’t effect the drawing.

edit: OK I looked at the code again and you call repaint in your actionPerformed() so that’s where your drawing and logic are connected.

Yes that is what I meant, sorry it was late and my brain ceased functioning. I’d still suggest you separate up your logic and rendering though using a loop to get decent time control. Note that your fix could be:

// Outside loop or timer somehow.
int delay = 0;

// Inside loop or timer.
if(keyInput.getKeyState(KeyEvent.VK_SPACE)) {
    if(++delay > 60) {
        delay = 0;
        spaceShip.fire();
    }
}

Which I think is a better alternative. This does mean your initial shot is delayed. Maybe you’d want to start at 60 to rectify this.

Hey, I really want to thank you, believe it or not, in 2018, you all solved my problem. I’m making a game in java and had exactly the same problem as you

[quote]when I hold press the space key to shoot, 2 things happen:
- at first it shoot one bullet and then it stops for about a second and then it continue shooting the other bullets
- while I’m shooting the bullets if I pressed the down or up keys to move the spaceship it stops shooting.
[/quote]
If it were not for you, I would still be looking for a solution. Thank you very very much!