3
votes

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");
        }
    }
}
1
Doesn't look like you should have a problem with the posted code. Are you sure this is all you have? - Adam Harte
Your load function seems rather useless as it doesn't return anything nor store anything beyond the scope of the function - BadFeelingAboutThis
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

1 Answers

4
votes

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.