0

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);
        }
    }
}
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

2 Answers2

1

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
  • 16,679
  • 4
  • 49
  • 69
  • 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 Gaban Sep 21 '09 at 16:21
  • 1
    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 Sep 21 '09 at 16:27
  • Ah, that's it thanks :) I'll edit my code once I get it working. – Leon Gaban Sep 21 '09 at 16:32
  • Now I'm getting "1120: Access of undefined property tc" :( Scope = my.pain – Leon Gaban Sep 21 '09 at 16:55
  • 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 Gaban Sep 21 '09 at 17:02
1

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
  • 420
  • 1
  • 5
  • 10