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

2015/9/9

Shoot

ozzaisswag ozzaisswag

2015/9/9

#
Can someone please tell how to shoot something out of an actor
Super_Hippo Super_Hippo

2015/9/9

#
Usually it looks like this:
private int bulletTimer = 0;

public void act()
{
    //...
    shoot();
}

public void shoot()
{
    if (bulletTimer < 50) //delay: smaller number = shoots faster
    {
        bulletTimer++;
        return;
    }
    
    if (Greenfoot.isKeyDown("space")) //shoots with the spacebar
    {
        Bullet b = new Bullet(); //or however you call the class
        getWorld().addObject(b, getX(), getY());
        b.setRotation(getRotation());
        b.move(5);
        bulletTimer = 0;
    }
}
You need to login to post a reply.