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

2016/5/27

Bullet Hoses

ZacBuzzsaw ZacBuzzsaw

2016/5/27

#
I've got a pistol and shotgun in my game. Unfortunately, the user, by holding the spacebar, can generate an infinite number of bullets. Since both weapons are intended to be semi-automatic, I've been trying to make use of a boolean to determine whether or not the code should generate a new Bullet actor. Code follows:
boolean isShot = false;

//code stuff that I've omitted b/c it's unnecessary

 if(Greenfoot.isKeyDown("space")) //Shooting mechanism
        {   isShot = true;         
            if(usingShotgun) //adds three bullets to the world for a shotgun-y effect
            {
                if(isShot = true)
            {
                getWorld().addObject(new Bullet(), this.getX(), this.getY()+30); 
                getWorld().addObject(new Bullet(), this.getX()-10, this.getY()+30);
                getWorld().addObject(new Bullet(), this.getX()+10, this.getY()+30);
            }
But this code, like the vast majority of my scenario, doesn't want to do what I want it to do. What am I doing wrong? I think it's the location of isShot (i.e. initializing it to "true" in the wrong place).
SPower SPower

2016/5/27

#
You're indeed setting isShot to true in the wrong place. Now you set it to true each time the user is holding down the spacebar, would would defeat the purpose of your variable. Also, in an if-statement, you're using the wrong symbol: == is a comparison, = is a changing of value. If you want to add a time gap between the firing of bullets, you're going to have to add a timer variable.
ZacBuzzsaw ZacBuzzsaw

2016/5/27

#
SPower wrote...
You're going to have to add a timer variable.
I've got a timer variable for weapon animations, like this:
long timer;
//more unnecessary code
if(Greenfoot.isKeyDown("space")) //Shooting mechanism
        {
            timer = System.currentTimeMillis();//Time in ms
            hasShot = true;

            if(usingPistol)
            {
                pistolShot.stop();
                this.setImage(shootPistol);
                pistolShot.play();
                getWorld().addObject(new Bullet(), this.getX(), this.getY()+50);
            }

            if(usingShotgun) //adds three bullets to the world for a shotgun-y effect
            {
                shotgunShot.stop();
                shotgunShot.play();
                getWorld().addObject(new Bullet(), this.getX(), this.getY()+30); 
                getWorld().addObject(new Bullet(), this.getX()-10, this.getY()+30);
                getWorld().addObject(new Bullet(), this.getX()+10, this.getY()+30);
                this.setImage(shootShotgun);
                timer = System.currentTimeMillis();//Time in ms
                if(System.currentTimeMillis() - timer > 200)
                    this.setImage(shotgun);
            }

            if(usingMinigun)
            {
                this.setImage(shootMinigun);
                getWorld().addObject(new Bullet(), this.getX(), this.getY()+70); 
                minigunShot.play();
                timer = System.currentTimeMillis();//Time in ms
                if(System.currentTimeMillis() - timer > 200)
                    this.setImage(minigun);
            }
Should I use that timer for my bullet gap, or do I need a different timer?
danpost danpost

2016/5/27

#
@SPower, I see where you see an assignment symbol ( '=' ) in place of a conditional equality symbol ( '==' ); however, it will never actually change the value there as it was just set to 'true' in a previous line. What this means is that the question does need to be asked. Removing lines 9, 10 and 14 will not change what the code does. Also, a timer is not what ZacBuzzsaw is going to need -- a boolean field is indeed what is wanted (like the 'isShot' field), but the way it is used is incorrect (hence, its name is also misleading). The field should track the state of the "space" key or should be named something like 'spaceDown' and its value should be changed anytime its current value is not the current state of the key. During a change in value, if changing to true, a shot can be fired:
if (Greenfoot.isKeyDown("space") != spaceDown) // did the key change states (currently pressed or released)
{
    spaceDown = !spaceDown; // change tracked state
    if (spaceDown) // was key currently pressed
    {
        if (usingShotgun)
        {
            // add objects here
You need to login to post a reply.