Hi everyone. I’m working in FlashDevelop and I have a question about an issue I’ve run into. (I posted this question in the FlashDevelop community and they recommended I bring the discussion over here).
I’m trying to design a space-like background in which particles fly down the screen. I have created a Background class and I’m using sprites to generate the particles. When they fly off-screen (say, when y > 420) I want them to disappear, but I can’t seem to figure out how. Here is the code for the Background class:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
public class Background extends Sprite
{
private var background:Sprite = new Sprite;
private var speed:int = 2;
private var _root:Object;
public function Background():void
{
_root = MovieClip(root);
addEventListener(Event.ADDED, create);
addEventListener(Event.ENTER_FRAME, move);
}
private function create(e:Event):void
{
var size:int = Math.random() * 10;
var position:int = Math.random() * 600;
background.graphics.drawRect(0, 0, size,size);
background.graphics.beginFill(0xFFED09, 1);
background.x = position;
background.y = -20;
addChild(background);
}
private function move(e:Event):void
{
this.y += speed;
if (this.y > 120)
{
removeEventListener(Event.ENTER_FRAME, move);
_root.removeChild(this);
}
}
}
}
The program will crash when y>120. (I’ve set 120 for ease of testing.)