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

2021/4/19

Hey guys how do I add a cool down to my gun, im making a game and this is my code for the arrow/bullet, how do I make it so there's a cool down between each bullet

TejasKanch TejasKanch

2021/4/19

#
public void act() { setLocation(getX() + speed, getY()); checkBoundaries(); destroyEnemies(); } //we add a method "checkBoundaries()" that destroys bullets that are off screen. public void checkBoundaries() { if(getX() > getWorld().getWidth() - 1) getWorld().removeObject(this); else if(getX() < 1) getWorld().removeObject(this); if(getY() > getWorld().getHeight() - 1) getWorld().removeObject(this); else if(getY() < 1) getWorld().removeObject(this); } //"destroyEnemies()" destroys enemies. public void destroyEnemies() { //"Enemy" can be any class that you want the bullet to destroy. Actor enemy = getOneIntersectingObject(foe1.class); if(enemy != null) { getWorld().removeObject(enemy); getWorld().removeObject(this); } } private int speed = 7; }
TejasKanch TejasKanch

2021/4/19

#
Nevermind guys I got it here you go: this goes in the actor that is shooting the bullet
 if(Greenfoot.isKeyDown("space") && (cooldown == 0)) {
                getWorld().addObject (new bullet (), getX()+0, getY()-2);
                cooldown = 10;
You need to login to post a reply.