package { import flash.display.MovieClip; import flash.events.Event; public class BackgroundHandler extends MovieClip { private var stars:Array = new Array(); private var playerShip:PlayerShip; private var noOfStars:int; private var stageWidth:int; private var stageHeight:int; public function BackgroundHandler($width:int, $height:int, $playerShip:PlayerShip) { playerShip = $playerShip; stageWidth = $width; stageHeight = $height; noOfStars = stageWidth * 0.2;//the bigger the view, the more stars for (var i:int = 0; i < noOfStars; i++) { stars[i] = new Star(Math.random() / 4); stars[i].x = Math.random() * $width; stars[i].y = Math.random() * $height; addChild(stars[i]); } addEventListener(Event.ENTER_FRAME, loop); } private function loop($e:Event):void { for (var i:int = 0; i < noOfStars && playerShip.HP > 0; i++) { stars[i].x -= (playerShip.SpeedX + playerShip.SpeedXOffset) * stars[i].Distance; stars[i].y -= (playerShip.SpeedY + playerShip.SpeedYOffset) * stars[i].Distance; //if star is outside the screen; move it to the opposite side if (stars[i].x > stageWidth)//right stars[i].x = 0; if (stars[i].x < 0)//left stars[i].x = stageWidth; if (stars[i].y > stageHeight)//below stars[i].y = 0; if (stars[i].y < 0)//over stars[i].y = stageHeight; } } private function radToDeg($radiant:Number):Number { return $radiant * (Math.PI / 180 ); } } }