Flash Horizontal Starfield
Simple 4Kb Parallax Scrolling Starfield, the viewable version contains extra functionality to control the amount of Stars and Starfield Speed. On-screen diagnostics have been displayed as they may help influence any changes you decide to make.
The Starfield can be seen on my sister site Flash Experiences it can be found here Flash Horizontal Starfield.
CALLING CODE
import com.horizontalstarfield.Starfield; var objStarfield:Starfield = new Starfield(); objStarfield.STAR_SPEED = 4; // By default this is currently set to 5. addChild(this.objStarfield);
STARFIELD CLASS
package com.horizontalstarfield
{
import flash.events.*;
import flash.display.*;
/**
* Name : Starfield
* Coded By : Matt Wakeling
* Date : 20th November 2011
* Description : Starfield Class for the main Starfield.
*
* @author Matt Wakeling
*/
public class Starfield extends Sprite
{
// Intialise Class Properties
private const START_STARS:int = 150;
private const MINIMUM_ALPHA_PERCENT:int = 10;
private const MAXIMUM_ALPHA_PERCENT:int = 100;
public var STAR_SPEED:int = 5;
// Starfield Constructor
public function Starfield()
{
// Constructor Code
super();
this.InitialiseStarfield();
}
// InitialiseStarfield Method
private function InitialiseStarfield():void
{
if (stage)
this.InitialiseStage();
else addEventListener(Event.ADDED_TO_STAGE, this.InitialiseStage);
}
// InitialiseStage Method
private function InitialiseStage(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, this.InitialiseStage);
var objStar:Star;
var intLoopCounter:int = 1;
while (intLoopCounter <= this.START_STARS)
{
objStar = new Star();
objStar.x = (Math.random() * stage.stageWidth);
objStar.y = (Math.random() * stage.stageHeight);
objStar.alpha = ((Math.floor((Math.random() * ((this.MAXIMUM_ALPHA_PERCENT - this.MINIMUM_ALPHA_PERCENT) + 1))) + this.MINIMUM_ALPHA_PERCENT) / 100);
objStar.STAR_SPEED = this.STAR_SPEED;
addChild(objStar);
objStar = undefined;
intLoopCounter++;
}
}
}
}
STAR CLASS
package com.horizontalstarfield
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
/**
* Name : Star
* Coded By : Matt Wakeling
* Date : 20th November 2011
* Description : Star Class for the Starfield Class.
*
* @author Matt Wakeling
*/
public class Star extends Sprite
{
// Intialise Class Properties
private const MINIMUM_ALPHA_PERCENT:int = 10;
private const MAXIMUM_ALPHA_PERCENT:int = 100;
private var intStageWidth:int;
private var intStageHeight:int;
public var STAR_SPEED:int = 5;
// Star Constructor
public function Star()
{
// Constructor Code
super();
this.InitialiseStar();
}
// Initialise Method
private function InitialiseStar():void
{
// Initiailise Stage
if (stage) this.InitialiseStage();
else addEventListener(Event.ADDED_TO_STAGE, InitialiseStage);
}
// InitialiseStage Method
private function InitialiseStage(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, InitialiseStage);
this.intStageWidth = stage.stageWidth;
this.intStageHeight = stage.stageHeight;
// Draw Star
this.graphics.clear();
this.graphics.beginFill(0xFFFFFF);
this.graphics.drawCircle(0, 0, 1);
addEventListener(Event.ENTER_FRAME, FrameEvent);
}
// FrameEvent Method
private function FrameEvent(e:Event):void
{
this.x -= alpha * STAR_SPEED;
if (this.x < 0)
{
this.x = this.intStageWidth;
this.y = Math.random() * intStageHeight;
alpha = ((Math.floor(Math.random() * (this.MAXIMUM_ALPHA_PERCENT - this.MINIMUM_ALPHA_PERCENT + 1)) + this.MINIMUM_ALPHA_PERCENT)/100);
}
}
}
}
Matt Wakeling (http://programmingexperiences.blogspot.com/)

0 Comments Received
Leave A Reply