title: Flash as3 referring to parent variables with child
body: <p>I'm having some trouble getting to grips with how a child can refer to the variables of parents. </p>
  
  <p>Here is my Main.as :</p>
  
  <pre><code>package
    {
          import flash.display.MovieClip;
  
      public class Main extends MovieClip
      {
          public var i;
          public var child: Child;
  
          public function Main()
          {
              i = 4;
              child = new Child;
          }
      }
  }
  </code></pre>
  
  <p>And here is my Child.as:</p>
  
  <pre><code>package
  {
      import flash.display.MovieClip;
  
      public class Child extends MovieClip
      {
          public function Child()
          {
              trace([parent as MovieClip].i);
          }
      }
  }
  </code></pre>
  
  <p>child is trying to access the public variable i in the parent, but I get the error:</p>
  
  <p>undefined</p>
  
  <p>Using trace(MovieClip(parent).i) instead I get: </p>
  
  <blockquote>
    <p>TypeError: Error #1009: Cannot access a property or method of a null
    object reference.     at Child()  at Main()</p>
  </blockquote>
  
  <p>I feel like I'm missing something pretty fundamental (like i hasn't been set a value by the time child is constructed) but I can't work out what.</p>
  
  <p>Clarification would be much appreciated. </p>