Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having some trouble simulating bullets in my 2D shooter. I want similar mechanics to Megaman, where the user can hold down the shoot button and a continues stream of bullets are fired but with a slight delay. Currently, when the user fires a bullet in my game a get an almost laser like effect. Below is a screen shot of some bullets being fired while running and jumping.

Bullets being fired

In my update method I have the following:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) ){
    bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Then I simply iterate through the array list increasing its x value on each update.

Moreover, pressing the shoot button (Space bar) creates multiple bullets instead of just creating one even though I am adding only one new bullet to my array list. What would be the best way to solve this problem?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You can add a little timer:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Basically you are noting the last time your character shot, then ensuring that enough time has passed before shooting again.

As for adding multiple bullets to the array list, it's doing this because you likely can't press the space bar fast enough to only have the space bar pressed for one frame. Each update you're adding one bullet. Add the delay like above will likely solve that problem. If you want to have a longer delay before you start repeating shots, you can doing something like:

if(gc.getInput().isKeyDown(Input.KEY_SPACE) && !previousInput.isKeyDown(Input.KEY_SPACE)) {
   startShootTime = gameTime.getTimeMS();
}
if(gc.getInput().isKeyDown(Input.KEY_SPACE) && 
 gameTime.getTimeMS() - shootTime > shootDelayInMilliseconds &&
 gameTime.getTimeMS() - startShootTime > initalShootDelayInMilliseconds ) {
   shootTime = gameTime.getTimeMS();
   bullets.add(new Bullet(player.getPos().getX() + 30,player.getPos().getY() +  17));  
}

Where initalShootDelayInMilliseconds is the time before holding the space bar causes repeated shots and shootDelayInMilliseconds is the time between each repeated shot thereafter. This is similar to the keyboard input for many text editors, holding a key for a certain amount of time will cause that key to repeat at a (often shorter) delay rate.

share|improve this answer
1  
if he is using a fixed timestep, he could also check current frame number and only update every N frames. eg: while(true) { if(currentframe%2 == 0) { //do something } }. –  tigrou May 27 '12 at 16:35
add comment

In Java this is the code if you use ArrayList..

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode(); ///get the key code of key pressed

    if (keyCode == KeyEvent.VK_SPACE) {
    if (!fireballs_array.isEmpty()) {  /// check if the array list is empty or not
        int last = fireballs_array.size()-1;  // get the last element index
        if(fireballs_array.get(last).getY() <550) //get y axis of last element
        {
            fire();
        }
    }
    else {
        fire();
    }
}
share|improve this answer
    
-1 Sorry but this won't solve any of the problems described. Maybe you should elaborate on why are you checking the Y coordinate of the last item in the array. And why against 550? –  Marton Nov 2 '12 at 10:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.