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

2013/4/11

Single shots

mufusion mufusion

2013/4/11

#
Hello, I have to Programm a game for a school project and decidet to do a shooter.I have the actor Spieler(player) and the actor Bullet. first i tryed to do the shooting with isKeydown but it shooted rapidly. then i tryed this code
1
2
3
4
5
6
7
public void Fire()
     {
         if ("space".equals(Greenfoot.getKey()))
         
        getWorld().addObject(new Bullet(), getX(), getY());
         
       }
now i can shoot single shots but when i hold space it CAN happen that the bullets been shot rapidly. how can i fix that? the code for Bullet is just that
1
2
3
4
5
6
7
8
public void act()
   {
       /**
        * die Location des Bullets wird ermittel und auf der x axe mit dem speed von 16 versetzt
        */
        
      setLocation(getX() + 16, getY());
   }
we have to write explanations as comments for our teacher, just ignore them.
danpost danpost

2013/4/11

#
You can you a combination of two conditions. The first condition is whether the "space" key is down or not. The other condition is what state the key was last detected as being in which will be controlled by a boolean instance field in the Spieler class.
1
2
3
4
5
6
7
8
9
10
11
12
// instance field to add
private boolean spaceDown;
// in act or a method it calls
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    spaceDown = true;
    getWorld().addObject(new Bullet(), getX(), getY());
}
if (spaceDown && !Greenfoot.isKeyDown("space"))
{
    spaceDown = false;
}
With this, the "space" key must be released and pressed again for another bullet to be created.
mufusion mufusion

2013/4/12

#
Thank you, its working now. is it possible that i can choose a pause between the shots? its too easy when the player press space really fast, it would be great if i can limit it to like 1 shot per second.
GreenGoo GreenGoo

2013/4/12

#
It is very easy. Simply have two variables, private int minGunFireDelay = 100; private int gunFireDelay = 0; private void fireGun() { if(gunFireDelay >= minGunFireDelay) { getWorld().addObject(new Bullet(getRotation()), getX(), getY()); gunFireDelay = 0; Greenfoot.playSound("Gunshot.wav"); } } In the act() method, write gunFireDelay++
mufusion mufusion

2013/4/16

#
now i have this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private int minGunFireDelay = 100;
private int gunFireDelay = 0;
 
     
    public void act()
    {
        checkKeypress();
        fire();
        fireGun();
        gunFireDelay++;
         
         
        
        
    }   
and this one
1
2
3
4
5
6
private void fireGun()
 { if(gunFireDelay >= minGunFireDelay)
     { getWorld().addObject(new Bullet(), getX(), getY());
         gunFireDelay = 0;
         }
       }
now it shoots automatically after the delay of "100", what does 100 stand for? and i dont want it to shoot automatically i want it to shoot when i press space but with a speed limitation ( no reload)
C.C.S C.C.S

2015/1/24

#
Try using
1
2
3
4
5
6
7
8
9
protected int fireDelay;
 
public void Fire()
     {
         if ("space".equals(Greenfoot.getKey()) && fireDelay > 8)
         fireDelay=0;
        getWorld().addObject(new Bullet(), getX(), getY());
          
       }
davmac davmac

2015/1/24

#
C.C.S wrote...
Try using
1
if ("space".equals(Greenfoot.getKey()) && fireDelay > 8)
Don't use Greenfoot.getKey(); use Greenfoot.isKeyDown(...). I.e. change the above line to:
1
if (Greenfoot.isKeyDown("space") && fireDelay > 8)
This will avoid soaking up other keystrokes that you might want to process, and will continue to return true while the key is held down (getKey() returns the key as it is typed, which is usually once when you first press it and then repeatedly after a delay).
Super_Hippo Super_Hippo

2015/1/24

#
@C.C.S: You also forgot the { and }. With your code, every act cycle a bullet would spawn. By the way, the last post of this thread was like nine month old.
C.C.S C.C.S

2015/1/24

#
davmac wrote...
C.C.S wrote...
Try using
1
if ("space".equals(Greenfoot.getKey()) && fireDelay > 8)
Don't use Greenfoot.getKey(); use Greenfoot.isKeyDown(...). I.e. change the above line to:
1
if (Greenfoot.isKeyDown("space") && fireDelay > 8)
This will avoid soaking up other keystrokes that you might want to process, and will continue to return true while the key is held down (getKey() returns the key as it is typed, which is usually once when you first press it and then repeatedly after a delay).
Something I didn't know. Thanks.
Super_Hippo wrote...
@C.C.S: You also forgot the { and }. With your code, every act cycle a bullet would spawn. By the way, the last post of this thread was like nine month old.
Oh. Didn't see. XD
You need to login to post a reply.