views:

37

answers:

3

This is driving me absolutely nuts. I've scoured threads on the subject and NOTHING seems to work.

I have an FLA file with the following code on frame 1:

import TestClass;

var tstClass:TestClass = new TestClass;

tstClass.testMethod();

In the accompanying AS file, I have the following:

package
{
    public class TestClass
    {
        public function testMethod():void
        {
            trace("It Works!");
        }
    }

}

I've tried everything I can think of to get this to work, but I keep getting error after error in Flash. The errors I'm getting are:

Scene 1, Layer 'Layer 1', Frame 5, Line 3 1180: Call to a possibly undefined method TestClass.

Scene 1, Layer 'Layer 1', Frame 5, Line 3 1046: Type was not found or was not a compile-time constant: TestClass.

Scene 1, Layer 'Layer 1', Frame 5, Line 1 1172: Definition TestClass could not be found.

Scene 1, Layer 'Layer 1', Frame 5, Line 1 1172: Definition TestClass could not be found.

I'd be hugely grateful to anyone that could offer ANY insight into a solution here.

Thanks, Jim

+2  A: 

You forgot the brackets:

var tstClass:TestClass = new TestClass();

P.S: Flash Builder is a 10 times nicer tool to code. And it can cooperate with Flash CS*.

Maxim Kachurovskiy
I'm afraid you're wrong. Parenthesis can be left out if the constructor takes no parameters. Check out http://livedocs.adobe.com/specs/actionscript/3/as3_specification131.html. Quote: "Arguments, if specified, are passed to the construct method. If no arguments are specified, the parentheses may be omitted."
Juan Pablo Califano
You are right, the problem is in classpath.
Maxim Kachurovskiy
A: 

You also need to create the TestClass() instantiation function in the TestClass class.

package
{
    public class TestClass
    {
        public function TestClass()
        {
            trace("class instantiated");
        }
        public function testMethod():void
        {
            trace("It Works!");
        }
    }
}

Then, use the the brackets like in Maxim Kachurovskiy's comment.

import TestClass;

var tstClass:TestClass = new TestClass();

tstClass.testMethod();
washwithcare
Maxim's answer is all you need to do. The constructor does not have to be explicitly written.
Allan
@Allan. Neither do the parens for a parameterless constructor.
Juan Pablo Califano
@Juan Pablo Califano Learn something new everyday :P
Allan
Thanks for your help guys, I think I've got everything working nicely now.
Jim Simpson
+2  A: 

From the error, it seems the compiler is not being able to find your class definition.

Double check your class path and that the package structure matches your directory structure.

The global classpath (for every file) can be set from: Edit -> Preferences -> Actionscript -> Actionscript 3.0 Settings.

The configuration for a particular fla can be edited from Publish Settings -> Settings.

If your fla and TestClass.as are in the same directory, make sure you have ./ (or just .) in your classpath.

Also check the name of the as file for typos and case differences (It should be named exactly as your class, with an .as extension)

Juan Pablo Califano