i know this is a stupid probably obvious question but after spending a ridiculous amount of time on it i give up. I have a class lets say Poker in an actionscript file called Poker.as. i want to use this class in an fla in the same directory. i instantiate it in the fla with the following line:
var myVar:Poker = new Poker();
This however gives me the errors:
1046: Type was not found or was not a compile-time constant: Poker.
1180: Call to a possibly undefined method Poker.
I have even tried doing "import Poker;" and that does nothing. What im doing i know works just fine in actionscript 2 but damn this actionscript 3. please help, i know this should be an easy answer, thanks in advance.
you need to define a classpath so that the compiler knows where to search for the AS file(s). To do this goto publish settings (F12?- not sure though) and somewhere there should stand compiler and or classpath.
If the Poker.as file is in the same directory as the fla then you do not need to import the classpath. You have another issue. From the looks of it you are doing everything right in instantiating you Poker Class. It is something simple in your setup somehow. Either it is not in the same directory or you have something named wrong like the constructor name in the class file. Double check everything and if you still can't find the problem post the files somewhere so we can take a look.
i seem to have gotten around my problem by putting my class into a package. but for future reference it would be really nice to know what was wrong with my previous code.
For reference to anyone that has a similar problem, ill explain in further detail. i simply took my Deck class and added "package poker {" before class declaration (obviously closing bracket at bottom of class too). I then put that class in a folder named "poker" and in my fla imported it using "import poker.*;". my directory structure was simply fla and poker directory in base folder with Deck.as in poker folder. Also made Deck class public along with any functions i needed to call. I also set classpath in publish settings to the base directory.
Your issue is a namespace issue. When you do not strictly define your classes, methods, variable members, etc they are put in the the default namespace of "internal". That being the case you were not allowed access to the class you created because it was not publicly available. All you needed to do to your old code was the following:
Code:
package{
public class TestClass {
protected var myVar:int;
public function TestClass() {
myVar = 5;
}
public function getMyVar():int {
return myVar;
}
}
}
It really a good habit to always use strict typing etc so that you never run into this sort of problem. I hope this helps.
Last edited by JHarlequin; 07-30-2007 at 01:49 PM.