title: AS3/Flixel: "Access of possibly undefined property."
body: <p>Pretty new flixel/AS3 user here, though not entirely new to coding. Anyways, the error is pretty straightforward in it's cause, but not it's solution. </p>
  
  <p>"Error: Access of possibly undefined property through a reference with static type org.flixel:FlxSprite." </p>
  
  <p>It gets thrown 6 times, twice with the property scrap.selected and four times with scrap.distanceToMouse - I've marked each line that throws an error with comments. </p>
  
  <p>I've searched throughout the web and StackOverflow specifically. It looks like a pretty common problem, but I haven't been able to apply any solutions to my specific situation. Anyways, Here's the relevant code... </p>
  
  <p>ScrapManager.as </p>
  
  <pre><code>    if (FlxG.mouse.pressed)
      {
          var ClosestDistance:int = 500; 
          for each (var scrap:FlxSprite in this)
          {
              scrap.selected = false; //error here!~ 
              var dx:int = scrap.x - FlxG.mouse.screenX;
              var dy:int = scrap.y - FlxG.mouse.screenY;
              scrap.distanceToMouse = (dx * dx) + (dy * dy); //error here!~
  
              if (scrap.distanceToMouse &lt; ClosestDistance) //error here!~
              {
                  ClosestDistance = scrap.distanceToMouse; //error here!~
              }
          }
          for each (var scrap:FlxSprite in this) 
          {
              if (scrap.distanceToMouse == ClosestDistance) //error here!~
              {
                  scrap.selected = true; //error here!~
              }
          }
      }
  </code></pre>
  
  <p>Scrap.as</p>
  
  <pre><code>package 
  {
      //import stuff...
  
      public class Scrap extends FlxExtendedSprite
          {
              public var selected:Boolean = false; 
              public var distanceToMouse:int; 
              //and more stuff...
  </code></pre>
  
  <p>I think (hope) that's all that's needed to solve this, but I'll be happy to provide more if needed. Thanks for reading. :D </p>