views:

758

answers:

2

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);
     }
    }
}
+1  A: 

You can't call a contructor like that. You'll need to do something like:

var tc = new TraceClass("hello");

EDIT: (after re-reading) Or, try TraceClass.TraceClass("hello");

Michael Todd
Hi Michael, thx for the reply... I tried both ways (the var I forgot about) but now I'm getting a "1061: Call to a possibly undefined method TraceClass through a reference with static type Class."
Leon
I think one of the main problems is that you're defining a class in a package, then trying to test that class in the same package, and I don't _think_ that's possible (not positive, though). I would try setting up a "main" class for testing, create a new instance of the TraceClass in it, and then try to test it via 'new' and then tc.TraceClass("hello");.
Michael Todd
Ah, that's it thanks :) I'll edit my code once I get it working.
Leon
Now I'm getting "1120: Access of undefined property tc" :( Scope = my.pain
Leon
Ah I Got it working! removed the variable from the main class function and moved "tc = new TradeFrame("hello");" into the main class and it works now. Thx for the help...
Leon
+1  A: 

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);
        }

}
Preston
Thanks master sensei!
Leon