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

2012/1/29

shoot

tylers tylers

2012/1/29

#
how do you have single shot firing
devjin1 devjin1

2012/1/30

#
I'm assuming you are using if (Greenfoot.isKeyDown(_key_)) ? Try changing it to the getKey() function, for example: if ("space".equals(Greenfoot.getKey())) shoot(); This will check if the space bar is pressed and will shoot a single shot rather than multiple.
tylers tylers

2012/1/30

#
where shall i put it
tylers tylers

2012/1/30

#
solted that out now but it fires 1 shot then if you hold it down it will fire loads of shots a second later. how do you stop that
Morran Morran

2012/1/30

#
You're trying to make it shoot only a few times a second? Try this: Add a variable of type "int" called timeTillCanFire in your player class:
//the beginning of your class, no changes here...
public class Player
{ 

   //your code should be here....

   //at the bottom of your player class, after all of your code, add this:
   private int timeTillCanFire;
}
After that, in your "fire()" method, do this:
public void fire()
{
   if(timeTillCanFire == 0)  { //if can fire now...
      if(Greenfoot.isKeyDown("space")) {
         //do whatever you normally do to acually fire.
         timeTillCanFire = 10; //wait ten frames before you can fire again.
      }
   }
   if(timeTillCanFire != 0) { //if can't fire now...
      timeTillCanFire--;      //wait till can
   }
}
You need to login to post a reply.