PDA

View Full Version : Explain package names please?


Chris_C
10-11-2007, 03:18 PM
Ok just say I have this in testFile.as

package {
public class testFile{
var phrase:String;
public function testFile(){
trace ("this part auto runs");
}

public function sayHello(phrase){
trace (phrase);
}
}
}


Now in the .fla file i just do a simple

var testtrace:testFile = new testFile();
testtrace.sayHello("Yarrrrr");


It works perfectly. However when i change the beginning of the .as file to

package anyname{

}


I cant figure out how to get it to work. What is the deal with package names?

I have many books now all which explain all sorts of wonderful things.. except the basics of package names and exactly how to use and reference them in a flash file. The thing that is covered well is when you're sending a movieclip to a class, but theres very little info on text based things like in my little example.

So in the short of please give me a full explanation "in detail" on how to use a class file when the package has a name.

Thanks!

senocular
10-11-2007, 04:33 PM
once in a package, two things have to happen.
1) You have to save the class file in a folder path that mirrors the package path. For example, if the class is in the package anyname and was saved in c:\myFlashProject\ (with the FLA) it would now have to be saved in c:\myFlashProject\anyname\. If the package was com.example.myfiles, it would have to be saved in c:\myFlashProject\com\example\myfiles\.

2) Once in a package, it is required that when you use your class, that class be imported into the current script using its package path. This can be done in the form of <packagepath>.* or <packagepath>.<classname>. For testFile in the package anyname, it would be imported using
import anyname.testFile;
Classes not in packages don't have to be imported because they are inherently recognized

Chris_C
10-11-2007, 05:16 PM
ahhh that makes much more sense now. so if i wanted to import multiple classes from a same folder i could import the same way using an * like import anyname.*; and it would pull in any class files I had at once right?

Thanks for explaining it!