Hey Everyone So I know this has to be really Simple I am over complicating it and can't figure out how to do it. So I have these stars moving in the background in my startScreen
and I want to remove them when the Game starts.
Here is How I have them setup in my Main Class:
public var numStars:int = 4;
//Add star background to startscreen only
for (var i:int = 0; i < numStars; i++)
{
stage.addChild(new mcStar(stage));
}
and in my mcStar
Class I have this code:
public class mcStar extends MovieClip
{
private var stageRef:Stage;
private var starSpeed:Number;
public function mcStar(stageRef:Stage)
{
this.stageRef = stageRef;
setupStar(true);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function setupStar(randomizeY:Boolean = false) : void
{
//Pick the frames for start position
var nRandom:Number = randomNumber(1, 3);
//Setup goto and stop on timeline
this.gotoAndStop(nRandom);
//inline conditional, looks complicated but it's not.
y = randomizeY? Math.random()*stageRef.stageHeight : 480;
x = Math.random()*stageRef.stageWidth;
alpha = Math.random();
//rotation = Math.random()*360;
//scaleX = Math.random();
//scaleY = Math.random();
starSpeed = 2 + Math.random()*2;
}
public function loop(e:Event) : void
{
y -= starSpeed;
if (y <= stage.stageHeight / 2)
setupStar();
}
public function destroyStar():void
{
if (this.parent)
{
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, setupStar);
removeEventListener(Event.ENTER_FRAME, loop);
}
}
//Generates a truly "random" number
function randomNumber(low:Number=0, high:Number=1):Number
{
return Math.floor(Math.random() * (1+high-low)) + low;
}
}
So what I am trying to accomplish is in my Main class when the user presses start and I remove the startScreen I want all the starts to be removed and stop spawning. But I can't figure out How I would go about removing them. When I try I always get a must be a child of the caller, etc...
any help would be appreciated thanks!
Store the stars in an array when you add it. So you can refer to them later.
The
destroyStar
-function is not best practice, because the star itself should not have the rights to change something in your main-displaylist. Better remove it directly inMain
.Main
mcStar
Further you dont need to pass the stage to the constructor. If a
mcStar
-instance is added on stage you can refer the stage (only ONE stage is existing) withthis.stage
Greetings.