This site requires JavaScript, please enable it in your browser!
Greenfoot back
massivassl
massivassl wrote ...

2017/12/2

Shooting at regular intervals

massivassl massivassl

2017/12/2

#
Hi guys, i need help. i wanted to make a PvP spaceship game with two players shooting at each other from the side of the screen. I have the shooting part down but i the bullets currently spawn in a stream that looks more like lazer at this point. my code for player 1 shooting currently looks like this
 public void shoot()
    {
        
        if (Greenfoot.isKeyDown("space"))
        { 
            getWorld().addObject (new Bullet(), getX(), getY());
           }
                
    }
How would i make them spawn at something like 1 second intervals?
danpost danpost

2017/12/2

#
Add a timer int field. Anytime a shot is fired, set it to some nominal value (maybe somewhere around 50 to 60 -- possibly). If the value is greater than zero, decrease it value by one. Only shoot if the key is down AND the value of the timer field is zero.
massivassl massivassl

2017/12/2

#
Timer int field? so it doesn't necessarily have to be a simpletimer class?
danpost danpost

2017/12/2

#
massivassl wrote...
Timer int field? so it doesn't necessarily have to be a simpletimer class?
No special class is needed. I mean, you could use a class for the timer, but a field is all that is needed provided you are not displaying the running values of the timer.
massivassl massivassl

2017/12/3

#
thanks a bunch
You need to login to post a reply.