3

I must be missing something simple here, but in my main app, I import my Pages class, which in turn imports and dynamically instantiates one of two page types. Unfortunatley it only results in the error: ReferenceError: Error #1065: Variable PageA is not defined. (when I call Pages.load("A");)

Pages

package pages 
{
    import pages.PageA;
    import pages.PageB;
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    public class Pages 
    {
        public static function load(pageType:String):void
        {
            var pageClass:Class = getDefinitionByName("pages.Page"+pageType) as Class;
        }
    }
}

PageA

package pages 
{
    import flash.display.Sprite;

    public class PageA extends Sprite 
    {
        public function PageA()
        {
            trace("PageA init");
        }
    }
}

PageB

package pages 
{
    import flash.display.Sprite;

    public class PageB extends Sprite 
    {
        public function PageB()
        {
            trace("PageB init");
        }
    }
}
jbalsas
  • 3,484
  • 22
  • 25
Devin Rodriguez
  • 1,144
  • 1
  • 13
  • 30
  • 1
    Doesn't look like you should have a problem with the posted code. Are you sure this is all you have? – Adam Harte Sep 05 '12 at 01:08
  • Your load function seems rather useless as it doesn't return anything nor store anything beyond the scope of the function – BadFeelingAboutThis Sep 05 '12 at 02:21
  • 1
    I've cropped out the code down to the minimum needed to reproduce the error. What I've determined is that because the classes aren't being declared in code, the compiler isn't including them, so when the application itself goes to load them, they are nowhere to be found. – Devin Rodriguez Sep 05 '12 at 02:33

1 Answers1

4

Exactly, the compiler plainly didn't include those classes in the compiled SWF. I've hit this wall somewhere before, when I've tried instantiating via generated string (in my case 'Gem'+an integer), and received about the same error. I went around it by creating a dummy constant, enumerating all the classes I plan to use, this made the compiler aware of this. So, make the following:

private static const PAGES:Array=[PageA, PageB];

And compile. Should do. Also, you don't need to import parts of "pages" package, they are already visible in your project, since your "Pages" class belongs to the same package.

Vesper
  • 18,599
  • 6
  • 39
  • 61
  • Genius! Perfect solution, much appreciated. Now that I've got that pesky problem out of the way I can continue development in FlashDevelop! – Devin Rodriguez Sep 05 '12 at 06:06