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

2016/5/17

Delay spawn of objects when holding down a specific key?

Sonarwave Sonarwave

2016/5/17

#
Hello everyone, I need some help with a game that I am making. You play as a turret where you have to hold down space (the set key) to shoot a stream of bullets at enemies. I have been able to make the bullets spawn wherever the front of the player is facing, but when I hold down space bar too many bullets spawn out from the turret. Does anyone know a way to slow down or reduce the spawn of the bullets when holding down space bar? thanks in advance :) here is the bullet spawn code in the turret actor
1
2
3
4
5
6
   if(Greenfoot.isKeyDown("space")){
   
  getWorld().addObject(new Bullet(getRotation()), getX(), getY());
  GreenfootSound sound = new GreenfootSound("turretsfx.wav");
  sound.play();
}
(This code is in the public void act section of the turret actor)
SPower SPower

2016/5/17

#
You could use the System.currentTimeMillis() function, which returns how many milliseconds have passed since January 1st 1970 (that's how you track time using Java). This function returns a long instead of an in (a long is a 64-bit integer, one could say; integers are 32 bit). For this, you'll need to know when the user 'first' pressed the space key (that is, before they weren't pressing, but now they are). A boolean is probably the simplest method for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// create some instance variables:
private boolean justSwitched; // you can call it anything you want, of course
private long fireTime; // the time the bullet was fired at
 
// then, in your act method:
if (Greenfoot.isKeyDown("space")) {
    if (!justSwitched) {
        fireTime = System.currentTimeMillis();
        justSwitched = true;
        fireBullet(); // you could past the code you posted, but I'd advice you to create a separate method for it.
    }
    if (fireTime +100 < System.currentTimeMillis()) { // instead of 100 milliseconds, you can choose any other delay you fancy
        fireBullet();
        fireTime = System.currentTimeMillis();
    }
} else {
    justSwitched = false;
}
You could also move the line fireTime = System.currentTimeMillis() to the fireBullet() method.
valdes valdes

2016/5/17

#
Here's another way, using Greenfoot cycles First, declare a member variable in your class
1
private int time = 0;
in your act method, or in other method called by act()
1
2
3
4
5
6
7
8
9
if (time <= 0) {
    if (Greenfoot.isKeyDown("space")) {
        getWorld().addObject(new Bullet(getRotation()), getX(), getY());
        Greenfoot.playSound("turretsfx.wav");
        time = 10;
    }
} else {
    time--;
}
Sonarwave Sonarwave

2016/5/17

#
SPower wrote...
You could use the System.currentTimeMillis() function, which returns how many milliseconds have passed since January 1st 1970 (that's how you track time using Java). This function returns a long instead of an in (a long is a 64-bit integer, one could say; integers are 32 bit). For this, you'll need to know when the user 'first' pressed the space key (that is, before they weren't pressing, but now they are). A boolean is probably the simplest method for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// create some instance variables:
private boolean justSwitched; // you can call it anything you want, of course
private long fireTime; // the time the bullet was fired at
 
Thanks for the fast reply!
 
Just tried that method, and everything is working except for "fireBullet();" which came up with the error "cannot find symbol - method fireBullet". I also tried out Valdes's method, but that didn't work unfortunately.
 
// then, in your act method:
if (Greenfoot.isKeyDown("space")) {
    if (!justSwitched) {
        fireTime = System.currentTimeMillis();
        justSwitched = true;
        fireBullet(); // you could past the code you posted, but I'd advice you to create a separate method for it.
    }
    if (fireTime +100 < System.currentTimeMillis()) { // instead of 100 milliseconds, you can choose any other delay you fancy
        fireBullet();
        fireTime = System.currentTimeMillis();
    }
} else {
    justSwitched = false;
}
You could also move the line fireTime = System.currentTimeMillis() to the fireBullet() method.
Sonarwave Sonarwave

2016/5/17

#
^^ I think my above post broke.... so here it is without the quote: Hello, thanks for the quick reply SPower! I tried out the code, and everything worked except for the "fireBullet" method which created the error "cannot find symbol - method fireBullet". I also tried out valdes's code, but that didn't slow the fire rate unfortunately :(
Sonarwave Sonarwave

2016/5/18

#
EDIT: I just fixed everything! the bullets are spawning with a delay now :D I had to delete the "fireBullet" methods and delete some of my existing code and replace it with the code SPower gave me, and that slowed down the spawn rate. thanks for the help you guys :) Here is the code for reference:
1
2
3
4
5
6
7
8
9
   if(Greenfoot.isKeyDown("space")){
            if(fireTime +50 < System.currentTimeMillis()){
  fireTime = System.currentTimeMillis();
        getWorld().addObject(new Bullet(getRotation()), getX(), getY());
  Greenfoot.playSound("turretsfx.wav");
} else{
  justSwitched = false;
}
}
SPower SPower

2016/5/18

#
I had put a comment in the code I posted, which explained that fireBullet was referring to the code you posted. But I'm glad it works now :)
You need to login to post a reply.