Serial and shooting game
              in 
             Integration and Hardware 
              •  
              1 year ago    
            
 
           
             Hello there!
            
            
I made a simple game where I have a spaceship that can fire against some enemies..
            
So far I press the CONTROL key to fire bullets.. The code below (at the end of the post) prevents the auto-fire. That means the spaceship fires a bullet each time the user presses the control key. Even if the user presses once the control key and holds it down then the spaceship fires only one bullet.
            
I also have an arcade button connected through arduino that I use it to fire bullets. Can anyone tell me how to prevent the auto-fire for this button? I mean for the keyboard keys there are the keyPressed() and keyReleased() but what is going on when you have a button like this?
            
Thanks!
            
            
if (spacebar==true && canfire==1) {
craft.craftMagazine.bullets.add(new Bullet(craft.get_direction()));
canfire=0;
}
            
            
void keyPressed() {
            
            
            
switch (keyCode)
{
case CONTROL:
spacebar = true;
break;
}
            
if (spacebar==true && canfire==1) {
craft.craftMagazine.bullets.add(new Bullet(craft.get_direction()));
canfire=0;
}
  
            
}
            
void keyReleased()
{
switch (keyCode)
{
case CONTROL:
spacebar = false;
            
canfire=1;
            
break;
}
}
 
           
 
            
           I made a simple game where I have a spaceship that can fire against some enemies..
So far I press the CONTROL key to fire bullets.. The code below (at the end of the post) prevents the auto-fire. That means the spaceship fires a bullet each time the user presses the control key. Even if the user presses once the control key and holds it down then the spaceship fires only one bullet.
I also have an arcade button connected through arduino that I use it to fire bullets. Can anyone tell me how to prevent the auto-fire for this button? I mean for the keyboard keys there are the keyPressed() and keyReleased() but what is going on when you have a button like this?
Thanks!
if (spacebar==true && canfire==1) {
craft.craftMagazine.bullets.add(new Bullet(craft.get_direction()));
canfire=0;
}
void keyPressed() {
switch (keyCode)
{
case CONTROL:
spacebar = true;
break;
}
if (spacebar==true && canfire==1) {
craft.craftMagazine.bullets.add(new Bullet(craft.get_direction()));
canfire=0;
}
}
void keyReleased()
{
switch (keyCode)
{
case CONTROL:
spacebar = false;
canfire=1;
break;
}
}
 
              
              1  
            
 
            
 
 
          