Question

I have an enemy class and a bullet class.

The enemies are added programmatically, and they handle their own movements within their class file.

How would I go about making them shoot bullets?

In the bullet class, there are some variables.

speed angle etc

But how can I get the proper angle? I need the angle to be based on the rotation of the particular enemy shooting the bullet.

So I need to add something like this to the bullet class file:

" if enemy is shooting blablaba addChild(this) angle = ((((((based on the enemies rotation?))))))

How do I do it? I have no idea how to refer to variables in other classes..

I know about _root., but that is irrelevant now.

No correct solution

OTHER TIPS

In this case, your Player might have a fire() function that deals with this.

As an extremely simple example of that, you might have:

public function fire():void
{
    var bullet:Bullet = new Bullet(x, y, rotation);
    stage.addChild(bullet);
}

Where your Bullet would accept the starting position and rotation:

class Bullet extends Sprite
{
    public function Bullet(x:int, y:int, rotation:Number)
    {
        this.x = x;
        this.y = y;
        this.rotation = rotation;
    }
}

This makes it easy to advance your fire() function to deal with things like the Player having a different weapon, no ammunition, etc:

public function fire():void
{
    if(currentWeapon.ammunition === 0) return;

    if(currentWeapon is Handgun)
    {
        // Make a HandgunBullet.
    }

    if(currentWeapon is Shotgun)
    {
        // Make a ShotgunBullet.
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top