title: AS3: Keep MovieClip on top
body: <p>So this is a little hard to explain, I am going to try my best but please just bear with me.</p>
  
  <p>OK. So I am creating a game that involves a road and cars move across it at a random time (every 5 seconds or so)</p>
  
  <p>I use the below code to create the cars and the direction they shall be travailing
  and remove once finished tweening across the screen.</p>
  
  <pre><code> public function animateCars()
      {
          CarMC = Random_Car();
  
          if (CarLeft)
          {
              CarMC.x = 0;
              CarMC.y = 245;
              CarMC.name = "CarNumber_"+CarNumber;
              addChildAt(CarMC,numChildren - 1);
              MoveCar = new Tween(CarMC,"x",None.easeNone,-60,900,12,true);
              MoveCar.addEventListener(TweenEvent.MOTION_FINISH, CarFinished);
              MoveCar.start();
          }
          else
          {
              CarMC.x = 0;
              CarMC.y = 258;
              CarMC.name = "CarNumber_"+CarNumber;
              CarMC.scaleX *=  -1;
              addChildAt(CarMC,numChildren - 1);
              MoveCar = new Tween(CarMC,"x",None.easeNone,800,-260,12,true);
              MoveCar.addEventListener(TweenEvent.MOTION_FINISH, CarFinished);
              MoveCar.start();
          }
          CarNumber++;
      }
  
      // Loads another car when finished
      function CarFinished(e:TweenEvent):void
      {
          removeChild(getChildByName("CarNumber_"+CarNumberR));
          CarNumberR++;
      }
  </code></pre>
  
  <p>This all works perfect <em>(well sorta but this is not the current issue</em>)*</p>
  
  <p>In this game you can also pick up and place objects. Using the below code:</p>
  
  <pre><code>    function addBuilding(Text:String)
      {
          HoldingBuilding = true;
          CursorPointer.visible = false;
          CursorGrab.visible = true;
          Building = new CarWash();
          Building.x = MouseX - MouseX % size;
          Building.y = MouseY - MouseY % size2;
          Building.rotation = 0;
          addChildAt(Building,numChildren);
      }
  </code></pre>
  
  <p>Again this works fine, when you hover the building over the car the car stays under it. (like it should)</p>
  
  <p>The problem's occur when you place the car too near the road, and for the first building it works fine but then if you add a second building the car will go under the newest building and over the oldest building.</p>
  
  <p>and I can't work out how I can keep the placed buildings on top <strong>or the cars on the bottom</strong></p>
  
  <p>This is the placing code:</p>
  
  <pre><code>function DropBuilding(e:MouseEvent):void
          {
              if (HoldingBuilding)
              {
                  if (BuildingPlacement)
                  {
                      HoldingBuilding = false;
                      BuildingPlacement = false;
                      CursorPointer.visible = true;
                      CursorGrab.visible = false;
  
                      Building.x = MouseX - MouseX % size;
                      Building.y = MouseY - MouseY % size2;
                      Building.rotation = 0;
                      Building.transform.colorTransform = NoColour;
                      Building.removeChild(BuildingBase);
                      BuildingBase = new Sprite();
                      BuildingShaddow = new Sprite();
                      ShakeTimer.stop();
                  }
                  else
                  {
                      ShakeTimer.start();
                  }
              }
          }
  </code></pre>
  
  <p>Thank you in advance for anything you can do to help keep either the buildings on top or the cars always on the bottom.</p>
  
  <p>Eli</p>
  
  <hr>
  
  <p>*Just incase you wondered this issue i have with the car tweening currently is every no and again (and i have not worked out why) one or two of the cars will just stop moving for 5 seconds then disappear.</p>