title: AS3 passing constructor parameters as variables in class
body: <p>Below is a class to generate citizens in a street. They walk both left and right and are assigned what side they will be facing, what speed they move and their y position. I previously used the function setUpCitizens() inside the constructor but I wanted to use ADDED_TO_STAGE and REMOVED_FROM_STAGE because I am having trouble removing the level and listeners.</p>
  
  <pre><code>package 
  {
  import flash.display.*;
  import flash.events.*;
  import flash.utils.getTimer;
  import flash.utils.Timer;
  
  public class Citizen extends MovieClip
  {
      private var dx:Number;// speed and direction
      private var lastTime:int;// animation time
  
      public function Citizen(side:String, _speed:Number, yPos:Number)
      {   
          var side = side;
          var speed = _speed;
          var yPos = yPos;
  
          addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
      }
  
      private function onAddedToStage(event:Event):void
      {
          setUpCitizens();
          addEventListener(Event.REMOVED_FROM_STAGE,    onRemovedFromStage);
      }
  
      private function onRemovedFromStage(event:Event):void
      {
          removeAllCitizens();
          this.removeEventListener(Event.ENTER_FRAME,moveCitizen);
      }
  
      private function setUpCitizens():void
      {
          if (side == "left")
          {
              this.x = -40;// start to the left
              dx = _speed;// walk left to right
              this.scaleX = 1;// reverse
          }
          else if (side == "right")
          {
              this.x = 5720;// start to the right
              dx =  -  _speed;// walk right to left
              this.scaleX = -1;
          }// not reverse
  
          this.y = yPos;// vertical position
          this.gotoAndStop(Math.floor(Math.random()*9+1));
          // set up animation;
          this.addEventListener(Event.ENTER_FRAME,moveCitizen);
          lastTime = getTimer();
      }
  
      private function removeAllCitizens():void
      {
          removeEventListener(Event.ENTER_FRAME,moveCitizen);
          parent.removeChild(this);
      }
  </code></pre>
  
  <p>I am getting this error:<br>
  <img src="https://i.stack.imgur.com/9LjcI.png" alt="errors"></p>
  
  <p>I call the class like this:</p>
  
  <pre><code>var c:Citizen = new Citizen(side,speed,yPos);
  </code></pre>
  
  <p>This is called from the parent class object Level1. I am also having trouble trying to reference all the generated children and remove them all to hopefully speed up the game...</p>
  
  <p>Thanks for any suggestions?</p>