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

2023/4/29

Creating a Game

Lolboy123 Lolboy123

2023/4/29

#
I want to have a bullet delay for the Shooter but I'm not sure how to code it Here is the code for Shooter: public class Shooter extends Actor { /** * Act - do whatever the Shooter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); if(Greenfoot.isKeyDown("P")) { shoot(); } } public void shoot() { getWorld().addObject(new Bullet(), getX(), getY()); } public Shooter() { GreenfootImage image = getImage(); image.scale(60, 40); setImage(image); } public void moveAndTurn() { int speed = 4; if(Greenfoot.isKeyDown("A")){ setLocation(getX() - speed, getY()); } if(Greenfoot.isKeyDown("D")){ setLocation(getX() + speed, getY()); } } } Here is the code for my bullets public class Bullet extends Actor { /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY() - 1); GreenfootImage image = getImage(); image.scale(50, 10); setImage(image); if(checkBoundaries()) EliminateThePolice(); } public boolean checkBoundaries() { if(getY()>555||getX()>555 ||getY()<5||getX()<5 ) { getWorld().removeObject(this); return false; } return true; } public void EliminateThePolice() { Actor Police = getOneIntersectingObject(Police.class); if(Police !=null) { getWorld().removeObject(Police); getWorld().removeObject(this); } } }
danpost danpost

2023/4/29

#
Lolboy123 wrote...
I want to have a bullet delay for the Shooter but I'm not sure how to code it << Codes Omitted >>
Just add an int shot delay timer to the Shooter class:
private int shotTimer;

public void act() {
    // ...
    if (shotTimer > 0) shotTimer--; // runs delay
    if (shotTimer == 0 && Greenfoot.isKeyDown("p")) {
        shoot();
        shotTimer = 30; // initializes delay
}
You need to login to post a reply.