A Flash Developer Resource Site

# Thread: [AS3] bullet spawning pattern help

1. ## [AS3] bullet spawning pattern help

I was curious if anyone knew of the most efficient way to spawn an arbitrary number of bullets from a specified point in a "fan" type pattern.

I've attached an image that helps illustrate what I mean:

So if I spawn one bullet, it more or less goes straight down, two bullets is split left and right, three has a left, a center, and a right, and so on.

It sort of makes me think of drawing a circle out of multiple points, but even that is a bit beyond me.

Any insight?

2. Figure out beforehand how many degrees you want between each bullet. When you fire, take your bullet count and multiply it by this number. That is your spread angle. If you had a space of 15 and 4 bullets, it would be a 60 degree spread. So, take your total spread and center it over the angle your pointing. In this case I'll use 270 degrees since it matches your images. Take that same total spread angle, cut it in half, and subtract it from 270. You get 240. Take the half again and this time add it to the 270. Take these two values as your start and end angle. Starting from the start value in a for loop, iterate to the end value, incrementing by your predetermined space between bullets. Here's an AS2 example: Angle Test 1

And here's the single-frame code, which should be self-explanatory as to what you need to use.
Code:
```onEnterFrame = run;

delay = 7;
timer = 0;

function run() {
timer++;
if (timer%delay == 0 && Key.isDown(Key.CONTROL)) {
fire();
}
dx = _xmouse - _root.spawn._x;
dy = _ymouse - _root.spawn._y;
xd = spawn._x-_root._xmouse;
yd = spawn._y-_root._ymouse;
rot = Math.atan2(yd, xd);
angle.text = rot*(180/Math.PI)+180;;
}

function fire() {
bc = Number(_root.bullets.text)-1;
trace("SA: " + sa + " || BC: " + bc);
if (!isNaN(bc) && !isNaN(sa)) {
var sh = sa/2;
var sp = sa/bc;
var s = 90 + sh;
var e = 90 - sh;
for (var i = s; i>=e; i-=sp) {
var ang = i;
var b = attachMovie("bullet", "b"+getNextHighestDepth(), getNextHighestDepth());
b.xc = 10*Math.cos(ang);
b.yc = 10*Math.sin(ang);
b._x = spawn._x;
b._y = spawn._y;
b.onEnterFrame = function() {
this._x += this.xc;
this._y += this.yc;
if (this._x < 0 || this._x > Stage.width || this._y < 0 || this._y > Stage.height) {
this.removeMovieClip();
}
}
}
}
}

stop();```
You ought to note that the method I provided just divides the bullets into the total spread to get the bullet spacing.

3. Thanks IP, this is pretty much exactly how I was thinking it would work.

I'll setup a special conditional for a single bullet so that it travels straight down, but otherwise this makes sense.

Cheers.

4. That's weird... I had it firing straight down with a single bullet... Let me check it again.

5. Code:
```onEnterFrame = run;

delay = 7;
timer = 0;

function run() {
timer++;
if (timer%delay == 0 && Key.isDown(Key.CONTROL)) {
fire();
}
dx = _xmouse - _root.spawn._x;
dy = _ymouse - _root.spawn._y;
xd = spawn._x-_root._xmouse;
yd = spawn._y-_root._ymouse;
rot = Math.atan2(yd, xd);
}

function fire() {
bc = Number(_root.bullets.text)-1;
st = bc * sa;
sp = st/2;
s = 90 + sp;
e = 90 - sp;
for (var i = s; i>=e; i-=sa) {
var ang = i;
var b = attachMovie("bullet", "b"+getNextHighestDepth(), getNextHighestDepth());
b.xc = 10*Math.cos(ang);
b.yc = 10*Math.sin(ang);
b._x = spawn._x;
b._y = spawn._y;
b.onEnterFrame = function() {
this._x += this.xc;
this._y += this.yc;
if (this._x < 0 || this._x > Stage.width || this._y < 0 || this._y > Stage.height) {
this.removeMovieClip();
}
}
}
}

stop();```
Here's test 2. Play with the numbers again. I got it right this time. Angle Test 2

You can see an example of a variable pointing angle by replacing the start/end variable lines with
Code:
```	s = (rot*rad2deg+180) + sp;
Then hold Control while you move the mouse.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap