-
Math class....available everywhere??? (scope)
I want to create a Class containing functions (similar to the Math class).
Also, like the Math class, I want it to be available everywhere.
For example, if I type
Code:
Math.floor(myNumber);
It works, no matter where I am in my code.
How can I make a class which has this ability?
I want to be able to type
Code:
MyMathClass.functionName();
and have it work, wherever I am in my code.
Is that possible??
Please help!
Cheers,
David
-
All you have to do is make sure that your class is on the default classpath. And if you don't want to put it in the default class path, you could always simply import it before using it.
In order to use functions defined in that class as you want to, those functions will need to be static. Static means that they belong to the class itself and not any particular instance of it.
-
Hopefully this works...
I haven't been able to test yet, but I believe the way to achieve what I wanted is to declare any variables or functions I want access to as "static".
For example below, I have set the function helloWorld() to static.
By importing this class, you should just be able to type Math3D.helloWorld(); without instantiating the class
Code:
package {
import flash.display.Sprite;
import flash.text.TextField;
public class Math3D {
public static function helloWorld():Sprite {
var myText:TextField = new TextField;
myText.text = "Hello world!";
var mySprite:Sprite = new Sprite();
mySprite.addChild(myText);
return mySprite;
}
}
}
-
Thanks for the reply, I just thought of using static the same time you posted
I also posted some source code to illustrate the point (which you have now confirmed as a viable method).
Cheers again! David
-
Palindrome emordnilaP
But now arises the question...why do all programmers use the "Hello World!" method as a default for a test so much O_O?? Just wondering so I can fill up my trivia bank...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|