Avatar britg

all the web, gaming, and foodie stuff that’s fit to printf()

Creating a Dynamic Starfield in Actionscript 3 Using the Flint Particle System

Spent a few hours this past weekend sqeezing every last bit of efficiency out of a starfield generator for Space Survivor.  Fortunately, this was made extremely easy with Richard Lord’s very fine Flint Particle System.

The basic concept:

  • Set up 4 particle emitters just outside the 4 edges of the screen
  • Feed the Starfield a velocity vector from your ship’s movement engine
  • use the velocity vector to give each generated particle a velocity opposite that of the ship
  • use the built-in garbage collector of the Flint Particle System to destroy stars that go off screen

There are still some issues with it, like pre-filling the screen with stars, but here’s a demo of it in action.  Code is below the fold:

To use the starfield generator, simply ad the starfield to your stage or a clip with:

  1. _starField = new StarField(_clip.stage, _width, _height, 0, 0);
  2. _clip.addChild(_starField.renderer);
  3. _starField.start();

Where _width and _height are the width and height of your viewport.
And then in your game loop:

  1. _starField.updateTargetVelocity(velX, velY);

Where velX and velY are the X and Y components of the velocity vector of your Object in Space.
And finally, here is the Starfield Class:

  1. package SpaceSurvivor.starfields
  2. {
  3.   import flash.display.Stage;
  4.   import flash.geom.Point;
  5.  
  6.   import org.flintparticles.actions.*;
  7.   import org.flintparticles.counters.*;
  8.   import org.flintparticles.displayObjects.Dot;
  9.   import org.flintparticles.emitters.Emitter;
  10.   import org.flintparticles.initializers.*;
  11.   import org.flintparticles.renderers.*;
  12.   import org.flintparticles.zones.*;
  13.  
  14.   public class StarField extends Emitter
  15.   {
  16.     private var _width:uint;
  17.     private var _height:uint;
  18.     private var _stage:Stage;
  19.     private var _targetVelocityAction:TargetVelocity;
  20.     private var _mult:int;
  21.     private var _starCount:Steady;
  22.  
  23.     private var _isPaused:Boolean = false;
  24.  
  25.     public function StarField(stage, width, height, velX, velY)
  26.     {
  27.       _width = width;
  28.       _height = height;
  29.       _stage = stage;
  30.  
  31.       renderer = new DisplayObjectRenderer();
  32.       _starCount = new Steady(50, 100);
  33.       counter = _starCount;
  34.  
  35.         addInitializer(new ImageClass(Dot, 1, 0×777777));
  36.         var multi:MultiZone = new MultiZone();
  37.         multi.addZone(new LineZone(new Point(-5,-5), new Point(_width+5,-5)));
  38.         multi.addZone(new LineZone(new Point(_width+5,-5), new Point(_width+5, _height+5)));
  39.         multi.addZone(new LineZone(new Point(_width+5, _height+5), new Point(-5, _height+5)));
  40.         multi.addZone(new LineZone(new Point(-5, _height+5), new Point(-5,-5)));
  41.  
  42.       addInitializer(new Position(multi));
  43.         addInitializer(new ScaleInit( 0.5, 1.5 ) );
  44.  
  45.       _targetVelocityAction = new TargetVelocity(velX, velY, 10);
  46.       addAction(_targetVelocityAction);
  47.       addAction(new Move());
  48.         addAction(new DeathOffStage());
  49.     }
  50.  
  51.     public function updateTargetVelocity(_velX, _velY):void
  52.     {
  53.       if(_velX == 0 && _velY == 0) {
  54.         pause();
  55.         _isPaused = true;
  56.       } else if (_isPaused) {
  57.         resume();
  58.         _isPaused = false;
  59.       }
  60.       _targetVelocityAction.targetVelocityX = -_velX;
  61.       _targetVelocityAction.targetVelocityY = -_velY;
  62.     }
  63.   }
  64. }

 
close Reblog this comment
blog comments powered by Disqus

Passion Projects