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

2019/1/22

Unsure how to make certain loop

Ni3ls Ni3ls

2019/1/22

#
I am trying to make a bar that slowly fills up while a delay goes down. I have made a class for a part of the bar.
public class BulletBlock extends Actor
{
    /**
     * Act - do whatever the BulletBlock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }   
    
    public BulletBlock()
    {
        drawBulletBlock();
    }
    
    public void drawBulletBlock()
    {
        GreenfootImage bulletblock = new GreenfootImage(3, 40);
        Color color = new Color (10, 10, 250, 200);
        bulletblock.setColor(color);
        bulletblock.fillRect(0,0,3,40);
        setImage(bulletblock);
    }
}
Now in a different class, LittleRedCapGun, I want to set up how and when to place one of those blocks. in the act() I have put a delay:
public void act() 
    {
        keys(3);
        shotDelay--;
        shootGun();
    }
when the delay is smaller than 1, you are able to shoot with this code, resetting it to 133
public void shootGun()
    {
        if (Greenfoot.isKeyDown("e") && shotDelay<1) {
            if (direction==1)
            {
                shoot(270);
            }
            if (direction==2)
            {
                shoot(0);
            }
            if (direction==3)
            {
                shoot(90);
            }
            if (direction==4)
            {
                shoot(180);
            }
            shotDelay=shotDelayStart;
        }
    }
Now I want to make a method in LittleRedCapGun using a loop to place more bulletblocks when the delay gets smaller, but I am unsure how to make that loop. Can anyone help me with this?
danpost danpost

2019/1/22

#
Unless I entirely misunderstand what you are wanting to do, I do not see why you would need a loop. On any given act step, the value of the shotDelay field will only go down once. If its value is not negative, you will only add one extra block into the world. On any act step a shot is fired, you would just remove all the blocks from the world. None of these actions requires a loop on your part.
You need to login to post a reply.