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

2015/1/20

Setting a Firing delay

howdoesonecode howdoesonecode

2015/1/20

#
as of right now my firing is one after another and ive tried alot of different code to fix it and none works Shooter(gun)
public class Shooter extends Actor
{
    public void act() 
    {
        if(Greenfoot.isKeyDown("space") )
        {
            getWorld().addObject(new bullet(getRotation()), getX(), getY());
        }
        if (Greenfoot.isKeyDown ("w"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown ("s"))
        {
            move(-3);
        }
        if (Greenfoot.isKeyDown ("d"))
        {
            turn(4);
        }
        if (Greenfoot.isKeyDown ("a"))
        {
            turn (-4);
        }
    }    
}
Bullet code
public class bullet extends Shooter
{
private int direction, speed;
    public bullet(int dir)
    {
        direction = dir;
    }


    int time=10;
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
 
    /**
     * 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()
    {
        setRotation(direction);
        move(speed);
    
        destroyEnemies ();
        move(17);
        kill();
        if (this.atWorldEdge()) 
        {
            getWorld().removeObject(this);
        }  
        }
    
 
    public bullet()
    {
        GreenfootImage image=getImage();
        image.scale(40,6);
        setImage(image);
    }  
 
    public void destroyEnemies()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor enemy = getOneIntersectingObject(Enemy.class);
        if(enemy != null) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(enemy); 
    }
}
    public void kill()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor blueenemy = getOneIntersectingObject(Enemy.class);
        if(blueenemy != null) 
        {
            getWorld().removeObject(blueenemy);                    
        }
    }

}
HELP!
danpost danpost

2015/1/20

#
How does one not code: by subclassing on type of object with a totally different type of object. That is, unless you can say that object A is a specific type of object B, then A should not subclass B. A bullet, which is of type projectile, can be shot from a gun, which is a weapon; but a bullet is not a gun. A Bullet class can extend a Projectile class, an Actor class and the Object class; and a Gun class can extend a Weapon class, an Actor class and the Object class; but a Bullet should not extend a Gun class. In the same sense, a Bullet class should not extend a Shooter class. Now, to add a delay between bullets created you will need to add an int field to the Shooter class as an act counter. When a bullet is created, set the field to some positive value. While its value is positive, decrement its value. Only create a bullet when its value is zero.
howdoesonecode howdoesonecode

2015/1/20

#
can you give me an example please
howdoesonecode howdoesonecode

2015/1/20

#
danpost wrote...
How does one not code: by subclassing on type of object with a totally different type of object. That is, unless you can say that object A is a specific type of object B, then A should not subclass B. A bullet, which is of type projectile, can be shot from a gun, which is a weapon; but a bullet is not a gun. A Bullet class can extend a Projectile class, an Actor class and the Object class; and a Gun class can extend a Weapon class, an Actor class and the Object class; but a Bullet should not extend a Gun class. In the same sense, a Bullet class should not extend a Shooter class. Now, to add a delay between bullets created you will need to add an int field to the Shooter class as an act counter. When a bullet is created, set the field to some positive value. While its value is positive, decrement its value. Only create a bullet when its value is zero.
can you give me an example showing where it needs to go etc.
danpost danpost

2015/1/20

#
In brief:
private int timer; // bullet creation delay timer

public void act()
{
    // possibly some non-related code here
    if (timer > 0) timer--; // run timer
    if (timer == 0 && Greenfoot.isKeyDown("space"))
    {
        /** create and add bullet into world */
        timer = 30; // start timer (adjust value as needed)
    }
    //  possibly some more non-related code here
}
howdoesonecode howdoesonecode

2015/1/21

#
danpost wrote...
In brief:
private int timer; // bullet creation delay timer

public void act()
{
    // possibly some non-related code here
    if (timer > 0) timer--; // run timer
    if (timer == 0 && Greenfoot.isKeyDown("space"))
    {
        /** create and add bullet into world */
        timer = 30; // start timer (adjust value as needed)
    }
    //  possibly some more non-related code here
}
it works perfectly thankyou my only problem now is getting a bullet to delete after it has intersected an enemy ive tried like a hundred different ways and nothing works any ideas?
danpost danpost

2015/1/21

#
howdoesonecode wrote...
my only problem now is getting a bullet to delete after it has intersected an enemy ive tried like a hundred different ways and nothing works any ideas?
Show what you have tried.
You need to login to post a reply.