开发者

In Flash, how do I import a package, instantiate the class, and call a method without getting an error?

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.


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*.


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)


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜