Search code examples
actionscript-3stage

AS3 - error 1120: Access of undefined property stage


I'm trying to add the QuickKong class to a game I'm making, as described here: http://www.kongregate.com/forums/90-kongregate-apis/topics/199155-quickkong-easy-kong-api-integration

To call the class, you use:

QuickKong.connectToKong(stage);

However, it keeps giving me:

error 1120: Access of undefined property stage.

Any suggestions? Thanks!


Solution

  • The stage is a property of a DisplayObject. When a DisplayObject is not on the Stage its stage property is undefined.

    So, you need to make sure the stage is available when you run QuickKong.connectToKong(stage);.

    If you do this in the constructor of your document class it should work just fine. Chances are you're trying to do this in some other class that doesn't have a stage property.

    If the class you're trying to run this in extends a DisplayObject such as MovieClip or Sprite you can listen for when it is added to the stage and then run your QuickKong code. Like this:

    package  {
    
    import flash.display.MovieClip;
    import flash.events.Event;
    
    public class Test extends MovieClip {
    
    
        public function Test() {
            addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }
    
        private function onAdded(e:Event):void{
            // now the stage is available
            QuickKong.connectToKong(stage);
        }
    }
    

    }

    Now, if for some reason you are not running your QuickKong code in a class that has access to the stage you should pass a reference to the stage into that class's constructor, like this:

    var someClass:SomeClass = new SomeClass(stage);
    

    Lastly, in your document class you could make a static variable reference the stage. Like this:

    package  {
    
    import flash.display.MovieClip;
    
    public class Main extends MovieClip {
    
    
        public function Main() {
            Main.stage = stage;
        }
    
    }
    

    }

    Now, you can just say: Main.stage anywhere in your code when you need to talk about the stage:

    QuickKong.connectToKong(Main.stage);