Search code examples
flashactionscript-3scopetracecoercion

Flash AS3: Getting this error: Access of undefined property - basic scope help needed :(


I'm still used to the AS2 style of all code on 1 frame, I'm trying to code AS3 in class files and I'm having a problem with a basic package setup. Scope issues are killing me with trying to learn AS3. Below is my package code, I don't have any other class files, just trying to return a simple trace.

The error I'm getting after I run the code below: 1120: Access of undefined property tc.


Main Class

package 
{
    import src.*;
    import flash.display.MovieClip;

    // Custom imports to go here
    import src.tradeclass.TradeFrame;   

    public class TraceClass extends MovieClip
    {
        public var tc:TradeFrame;

        public function TraceClass(traceText:String):void
        {
            // Constructor function
        }

    }

    tc = new TradeFrame("hello");
    //TraceClass.TradeFrame("hello");

}

Sub Class

package src.traceclass 
{
    import src.*;
    import flash.display.MovieClip;

    public class TradeFrame extends MovieClip
    {

        public function TradeFrame(traceText:String):void
        {
            // Constructor function
            trace(traceText);
        }
    }
}

Solution

  • Main Class needs to be:

    package src 
    {
    
    import flash.display.MovieClip;
    
    // Custom imports to go here
    import src.tradeclass.TradeFrame;       
    
    public class TraceClass extends MovieClip
    {
            public var tc:TradeFrame;
    
            public function TraceClass(traceText:String = "default text"):void
            {
                    // Constructor function
                  tc = new TradeFrame("hello");
    
            }
    
    }
    

    Sub Class needs to be:

    package src.tradeclass {
    
    import flash.display.MovieClip;
    
    public class TradeFrame extends MovieClip
    {
    
            public function TradeFrame(traceText:String):void
            {
                    // Constructor function
                    trace(traceText);
            }
    
    }