A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [AS3] bullet spawning pattern help

  1. #1
    bibuti. nolen's Avatar
    Join Date
    Sep 2002
    Location
    az.
    Posts
    191

    [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?
    i'm obsessed with video games.

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    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;
    
    deg2rad = Math.PI/180;
    rad2deg = 180/Math.PI;
    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;
    	sa = Number(_root.spreadAngle.text);
    	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; 
    			ang *= deg2rad;
    			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.
    Last edited by ImprisonedPride; 04-29-2009 at 06:51 PM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    bibuti. nolen's Avatar
    Join Date
    Sep 2002
    Location
    az.
    Posts
    191
    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.
    i'm obsessed with video games.

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    That's weird... I had it firing straight down with a single bullet... Let me check it again.
    Last edited by ImprisonedPride; 04-29-2009 at 07:25 PM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  5. #5
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Code:
    onEnterFrame = run;
    
    deg2rad = Math.PI/180;
    rad2deg = 180/Math.PI;
    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*rad2deg+180;;
    }
    
    function fire() {
    	bc = Number(_root.bullets.text)-1;
    	sa = Number(_root.spreadAngle.text);
    	st = bc * sa;
    	sp = st/2;
    	s = 90 + sp;
    	e = 90 - sp;
    	for (var i = s; i>=e; i-=sa) {
    		var ang = i; 
    		ang *= deg2rad;
    		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;
    	e = (rot*rad2deg+180) - sp;
    Then hold Control while you move the mouse.
    Last edited by ImprisonedPride; 04-29-2009 at 07:29 PM.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center