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

2024/6/5

Help with bullets

sussywussy sussywussy

2024/6/5

#
I need help with how to code a gun in GreenFoot. Currently, here is my code: For the bullets class:
public boolean check()
    {
        return (isAtEdge() || isTouching(armory.class));
    }
    public void turn()
    {
        MouseInfo info = Greenfoot.getMouseInfo();
        if (info != null)    turnTowards(info.getX(), info.getY());
    }
    public void fire()
    {
        if(!turned)
            {
                turn();
                turned = true;
            }
            if(fired)
            {
                move(speed);
                if(check())
                {
                    getWorld().removeObject(this);
                    fired = false;
                }
            }
    }
for the pistolBullet class, which extends the bullets class (the super(15) sets the speed to 15):
public pistolBullet()
    {
        super(15);
    }
    public void act()
    {
        if((pistol.has || AK.has) && sigma.noPbullets > 0)
        {
            fire();
        }
    }
and, for how I fire it (sigma is my main character):
else if(pistol.has)
        {
            if (Greenfoot.mousePressed(null) && sigma.noPbullets > 0 && guns.cooldown > 50)
            {
                pistolBullet p = new pistolBullet();
                addObject(p, sigma.X, sigma.Y);
                p.setFired(true);
                sigma.noPbullets--;
                guns.cooldown = 0;
            }
        }
the problem is I want to be able to have multiple bullets on the screen at once, but it doesn't work. please help!!!!!
danpost danpost

2024/6/6

#
sussywussy wrote...
<< Code Omitted >>
Please provide the complete class codes for the bullets and pistolBullet classes.
sussywussy sussywussy

2024/6/6

#
Nevermind, it works. Thanks danpost!
You need to login to post a reply.