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

2017/5/18

Method Interval Delay

zeke.shoe zeke.shoe

2017/5/18

#
Hi, I'm relatively new to programming and I seem to be having a weird interval delay with some of my methods. The method affected is the one that makes the character in my game shoot and every now and then when i try tapping the button to shoot (l or v) it wont immediately fire and ill have to press it again. One thing I have noticed is changing the value of act() line 5 in the if statement makes the delay either larger or smaller, but is there anyway to get rid of it entirely?
 public void act() 
    {
        count++;
        move();
        if(count > 20) 
        {
            shoot();
            count = 0;
        }
    }
 public void shoot()
    {
        if(Greenfoot.isKeyDown("l") && ammo > 0 && isPlayer1)
        {
            bullet c = new bullet(false);
            getWorld().addObject(c, getX(), getY());
            c.setRotation(getRotation()-90);
            ammo--;
        }
        if(Greenfoot.isKeyDown("v") && ammo > 0 && !isPlayer1)
        {
            bullet c = new bullet(true);
            getWorld().addObject(c, getX(), getY());
            c.setRotation(getRotation()-90);
            ammo--;
        }
    }
Yehuda Yehuda

2017/5/18

#
The whole if statement on line 5 in the act method is only for a delay. If you don't want a delay by shooting, then just replace lines 3 - 9 with:
move();
shoot();
zeke.shoe zeke.shoe

2017/5/18

#
My bad, I seem to have forgot to explain something. I want a delay between the creation of the bullets, but the problem I'm having is if I tap the key sometimes if i don't hold it for at least a second it won't create a bullet, as if count was less than 20, even though by the amount of times act would've been passed count should have incremented to be greater than 20. @Yehuda
danpost danpost

2017/5/18

#
Cut line 8 in the first code set and paste (insert) it within both if blocks of the second code set. You only want to reset the field when you actually do shoot.
zeke.shoe zeke.shoe

2017/5/18

#
Omg thank you so much! I can't believe I overlooked such a simple fix.
You need to login to post a reply.