View Full Version : Working with external class files.
simonj
December 6th, 2008, 08:20 AM
So, here is my situation.
I have a structure that looks like this.
main.fla
org
-mysite
--documentClass
---DocumentClass.as
--library
---XMLInit.as
---MenuInit.as
--objects
---XMLmenu.as
First I put all the code in the DocumenClass.as (except the one in XMLmenu.as, since that one is loaded via a MovieClip in the scene), but quickly this became ugly and confusing, so I split it up in different classes, XMLInit should be called from the DocumentClass.as, and MenuInit should be called from the XMLInit file.
And here is where I am stumped.
I've included the classes like this in the documentclass.as:
import org.mysite.library.*;
and in the XMLInit file I have this line:
import org.mysite.library.MenuInit;
Everything is compiled without errors, but I can't call the functions inside XMLInit and MenuInit, I either get "1136: Incorrect number of arguments. Expected 1.
Valaran
December 6th, 2008, 11:27 AM
The error is related to the function itself, not importing of the .as file. As it says, Incorrect number of arguments, expecting one. Meaning you cannot call the function like:
whateverfunction();
It requires some arguments, like:
whateverfunction(a, b, c);
simonj
December 6th, 2008, 06:23 PM
The error is related to the function itself, not importing of the .as file. As it says, Incorrect number of arguments, expecting one. Meaning you cannot call the function like:
whateverfunction();
It requires some arguments, like:
whateverfunction(a, b, c);
No, inside the XMLInit.as file (which I've imported), I've got a function called XMLfunc, which right now just states:
public function XMLfunc() {
trace("clicked");
}
I want to be able to call this function from my DocumentClass.as script, but I can't since is says "1180: Call to a possibly undefined method XMLfunc.
scottc
December 6th, 2008, 06:32 PM
This may help you on your quest:
http://www.kirupa.com/forum/showthread.php?t=313070
Im guessing that your calling the method from the class incorrectly... so you'll have to show us those lines instead.
var s:Sprite = new Sprite();
s.method();
//or for static functions..
Sprite.method();
simonj
December 6th, 2008, 07:04 PM
This may help you on your quest:
http://www.kirupa.com/forum/showthread.php?t=313070
Im guessing that your calling the method from the class incorrectly... so you'll have to show us those lines instead.
ActionScript Code:
var s:Sprite = new Sprite();
s.method();
//or for static functions..
Sprite.method();
Ehum, I'm not really sure. I think I missed some major part here.
Can you take time to tell me how to write a really simple external .as file (class). And how to call it from a document class file? Or even better, if someone could give me a link to a basic tutorial covering this, because I've tried finding one, but it almost seems like it's to basic for people to write about.
Thanks for your time. :)
Valaran
December 6th, 2008, 08:28 PM
Hello again, I'll give you some code to bite into, I'm not sure how familiar you are with AS3, so I just commented everything.
Note: I haven't actually tried this out, but I'm fairly confident it would work.
package {
// Import the class
import External_ExampleClass;
// Import the function
import External_ExampleFunction;
public class MainExample {
// Variable holding the external class
private var external:External_ExampleClass;
public function MainExample(){
useClass();
//useFunction();
}
private function useClass():void {
// Instantiating the class
external = new External_ExampleClass();
// Call the traceString() function which traces "Clicked!"
external.traceString();
// Call the getString() function which returns "Clicked!" to the variable str
var str:String = external.getString();
// Trace str to output "Clicked!"
trace(str);
}
private function useFunction():void {
// Call the imported function from External_ExampleFunction.as
External_ExampleFunction();
}
}
}
// External_ExampleClass.as
package {
public class External_ExampleClass {
public function External_ExampleClass(){
trace("Hello, I'm an external class!")
}
public function traceString():void {
trace("Clicked!");
}
public function getString():String {
return "Clicked";
}
}
}
// External_ExampleFunction.as
package {
public function External_ExampleFunction(){
trace("Clicked!");
}
}
One very important factor here is the naming of the .as files after the function/or class. If you want to import a function from its own .as file, there should be a single function in the document, and the function shall have the same name as the file (without the .as of course)
For classes it's the same, it should have the same name as the file, but as you probably know, there is no limit (as far as I know) to how many functions a class can have.
Hope that helped,
Matt
scottc
December 6th, 2008, 08:51 PM
senocular has a pretty nice tutorial...
http://www.senocular.com/flash/tutorials/as3withflashcs3/
although it can be alot to take in at once...
as3 is pretty much all about the classes...
Once you learn how to use them, its pretty straight forward from there..
simonj
December 7th, 2008, 06:36 AM
Thank you so much Valaran for the code, and thank you Scott for the link. I've only just started with AS3 but got some experience with other OOP-languages. I'll bookmark that link, seems like a good place to check out when I get stuck.
But that is for now, got it working :)
Thanks again!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.