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

2017/4/27

How do i make a delay when shooting

Ozan31 Ozan31

2017/4/27

#
public void shoot()
{
       if ("space".equals(Greenfoot.getKey()))
        {
            ball shot=new ball();
            getWorld().addObject(shot,getX(),getY());
            shot.setRotation(getRotation());
        }
}
danpost danpost

2017/4/27

#
private int shotTimer;
private boolean spaceDown;

public void shoot()
{
    if (shotTimer > 0 && --shotTimer > 0) return; // if timer running and not yet zero, exit method
    if (spaceDown != Greenfoot.isKeyDown("space")) // if change in state of trigger
    {
        spaceDown = !spaceDown; // record new trigger state
        if (spaceDown) // if new trigger state is down
        {
            ball shot = new ball();
            getWorld().addObject(shot, getX(), getY());
            shot.setRotation(getRotation());
        }
    }
}
Ozan31 Ozan31

2017/4/27

#
Thanks for that, i appreciate your help.
You need to login to post a reply.