PDA

View Full Version : removeChild problem



reblis
June 15th, 2008, 10:12 PM
I'm trying to remove a linked MC once it reaches a certain point on the stage, but it seems to remove all the MC's. How do I single out just the specific one that is at that stage position?

Here is my code:



package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import caurina.transitions.*;

public class DocumentClass extends MovieClip
{
private var timer:Timer = new Timer(100);

public function DocumentClass():void
{
timer.addEventListener(TimerEvent.TIMER, makeStar);
timer.start();
}

private function makeStar(e:TimerEvent):void
{
var star:Star = new Star();
star.scaleX = star.scaleY = Math.random();
star.x = Math.random() * this.stage.stageWidth
star.y = 400;
Tweener.addTween(star, {y:-20,time:5, transition:"easeoutExpo"});
this.addChild(star);

if(star.y >= 10)
{
trace("this is working");
/*this.removeChild(star);*/
}

}
}
}

amarghosh
June 16th, 2008, 12:58 AM
private function makeStar(e:TimerEvent):void
{
var star:Star = new Star();
star.scaleX = star.scaleY = Math.random();
star.x = Math.random() * this.stage.stageWidth
star.y = 400;
Tweener.addTween(star, {y:-20,time:5, transition:"easeoutExpo"});
this.addChild(star);
if(star.y >= 10)
{
trace("this is working");
/*this.removeChild(star);*/
}
}
read your code carefully.
u r setting star.y = 400;
then removing star from stage if star.y is greater than 10 (which is always true);
hence none of the stars remain on the stage :)

first set star.y = random value;
then add:

star.addEventListener(Event.ENTER_FRAME, checkYandRemove);
outside makeStar method:

private function checkYandRemove(e:Event):void
{
if(DisplayObject(e.target).y >= 10)//r u sure u wanna remove at 10? or is it 400?
{
removeChild(DisplayObject(e.target));
}
}

reblis
June 17th, 2008, 12:00 AM
maybe I wasn't clear in my code or my explanation.

When the stars reach a certain height I want them to be removed from the stage.
I set this to y=10 for this so you can see them, in the final movie they will be removed once they are at some point off stage.




package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import caurina.transitions.*;

public class DocumentClass extends MovieClip
{
private var timer:Timer = new Timer(100);

public function DocumentClass():void
{
timer.addEventListener(TimerEvent.TIMER, makeStar);
timer.start();
}

private function makeStar(e:TimerEvent):void
{
var star:Star = new Star();
star.scaleX = star.scaleY = Math.random();
star.x = Math.random() * this.stage.stageWidth
star.y = 400;
Tweener.addTween(star, {y:20,time:5, transition:"easeoutExpo"});
this.addChild(star);
}
}
}


I need to to remove just one instance the specific star that is at that point not all of them.

Any extra insight would be appreciated.

~reblis