ActionScript 3.0: A Simple StarField

I’ve been pootlin’ about with Flash a little behind the scenes of late, just making the occasional graphical trinket, and it struck me that I hadn’t made a starfield yet in ActionScript – so I knocked together a quick and dirty one in about 20 minutes…

The “star” itself is just a really small, white circle – so small that it turns into a single-pixel block with filtering on it – you can change out the symbol to anything you like, or just draw actual points if you wanted to – I’m happy enough that the guts of it work and will leave it alone and re-use the code when I want the effect sometime…

Source code after the jump if that’s your thing…

Flash File Source Code:

stage.quality = StageQuality.MEDIUM;
 
var starVect:Vector.<Star> = new Vector.<Star>;
 
var motionPaused:Boolean = true;
 
stage.addEventListener(MouseEvent.CLICK, addStars);
 
var banner:MessageBanner = new MessageBanner();
banner.x = stage.stageWidth  / 2;
banner.y = stage.stageHeight / 2;
addChild(banner);
 
function addStars(e:MouseEvent):void
{
	// If we're currently paused...
	if (motionPaused == true)
	{
		// Flip the pause flag
		motionPaused = false;
 
		// Remove the banner from the stage
		removeChild(banner);
 
		for (var loop:int = 0; loop < 400; loop++)
		{
			var tempStar:Star = new Star(this.stage);
 
			starVect.push(tempStar);
 
			addChild(starVect[loop]);
		}
	}
	else // If we're currently playing...
	{
		// Flip the pause flag....
		motionPaused = true;
 
		// Put the banner back on the stage
		addChild(banner);
 
		// And remove all the stars
		while (starVect.length > 0)
		{
			starVect[0].starDestructor(); // Unbind the event listener for the star...
			removeChild(starVect[0]);     // ...then remove it from the stage...
			starVect.splice(0, 1);        // ... and finally from the vector.
		}
 
	} // End of else condition
 
} // End of addStars function

Star Class Source Code:

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Stage; // Used to pass the main stage to the constructor
 
	public class Star extends Sprite
	{
		// Class properties
		public var zSpeed:Number;
		public var zRotationSpeed:Number;
		public var fadedIn:Boolean;		
 
		// Class variables
		public static var maxX:int = 200;
		public static var minX:int = 100;
		public static var maxY:int = 200;
		public static var minY:int = 100;
		public static var maxZ:int = 400;
		public static var minZ:int = 200;
		public static var midStageX:int;
		public static var midStageY:int;
 
		// Constructor
		public function Star(theStage:Stage):void
		{
			// Get the middle of the stage
			midStageX = theStage.stageWidth  / 2;
			midStageY = theStage.stageHeight / 2;
 
			// Randomise x, y and z position for each star
			this.x = ((Math.random() * maxX) - minX) + midStageX;
			this.y = ((Math.random() * maxY) - minY) + midStageY;
			this.z = (Math.random()  * maxZ) - minZ;
 
			// Set random z speed and rotation for each star
			this.zSpeed = (Math.random() * 5) + 3;
			this.zRotationSpeed = Math.random() * 2;
 
			// Make each star completely transparent and set the fadedIn flag
			this.alpha = 0;
			this.fadedIn = false;
 
			// Bind each star to update once per frame
			this.addEventListener(Event.ENTER_FRAME, updateStar);
		}
 
		// Function to unbind the event listener of the star
		public function starDestructor():void
		{
			this.removeEventListener(Event.ENTER_FRAME, updateStar);
		}
 
		// Function to update the star each frame
		private function updateStar(e:Event):void
		{
			// Move star closer...
			this.z -= this.zSpeed;
 
			// Rotate star
			this.rotation += this.zRotationSpeed;			
 
			// If we're getting really close start to fade the star out
			if (this.z < -470)
			{
				this.alpha -= 0.2;
 
				// If the star is super-close reset it
				if (this.z < -800) 
				{
					// Re-randomise x, y and z locations
					this.x = ((Math.random() * maxX) - minX) + midStageX;
					this.y = ((Math.random() * maxY) - minY) + midStageY;
					this.z = (Math.random()  * maxZ) - minZ;
 
					// Reset the alpha to completely transparent
					this.alpha = 0;
					this.fadedIn = false;
 
				} // End of reset if statement
 
			} // End of if star is close statement
 
			// Increase the star alpha to fade it in if it's not already faded in
			if (this.fadedIn == false)
			{
				this.alpha += 0.01;
 
				// If the star's entirely faded in, set the flag to say so
				if (this.alpha >= 1)
				{
					this.fadedIn = true;
				}
			} // End of if statement
 
		} // End of updateStar function
 
	} // End of class
 
} // End of package

The source code and flash file can be found: here.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.