title: Cast a stage child movieclip into a custom class extends movieclip AS3
body: <p>I'm creating a program where I add multiple Movieclips to the stage in Flash, instance name them 'item_#' per frame, and at runtime I pull these children into an array and dynamically cast them to a custom class called 'Item' which extends MovieClip.</p>
  
  <pre><code>function loadUpItems(): Array //loads items into ItemArray{
  var ItemArray:Array = new Array(); //Dynamically holds the items_# for each scene
  
  for(var i:int = 0; i&lt;numChildren; i++) //loop number of children on stage
  {
  
      if(getChildAt(i).name.substr(0,4) == "item") //if child is an item
      {
          trace("Item Child is: " + getChildAt(i));
          var mc:Item = new Item();       
          mc = getChildAt(i) as Item;
          mc.setItemX(getChildAt(i).x);
          mc.setItemY(getChildAt(i).y);
          mc.setItemNumber(Number(getChildAt(i).name.substr(5)));
          ItemArray[getChildAt(i).name.substr(5)] = mc; //Add to Items:array as instance numbered
          ItemArray[getChildAt(i).name.substr(5)].stop();
          trace("Loaded Item : " + ItemArray[getChildAt(i).name.substr(5)].itemName() + " : Frame : " + CurrentFrame);
      }
  }
  trace("ItemArray : " + ItemArray);
  return ItemArray;}
  </code></pre>
  
  <p>The problem being I can't seem to cast them properly as I get a null at the point:</p>
  
  <pre><code>mc = getChildAt(i) as Item;
  </code></pre>
  
  <p>It worked when I was just pulling the items as MoviClips and didn't need to cast them:</p>
  
  <pre><code>mc = getChildAt(i) as MovieClip;
  </code></pre>
  
  <p>Help welcome,</p>
  
  <p>Cheers,</p>
  
  <p>Dan</p>