dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Math class....available everywhere??? (scope)

Hybrid View

  1. #1
    Member
    Join Date
    Mar 2004
    Posts
    45

    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

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  3. #3
    Member
    Join Date
    Mar 2004
    Posts
    45
    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

  4. #4
    Member
    Join Date
    Mar 2004
    Posts
    45

    Lightbulb 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;
    		}
    		
    	}
    	
    }

  5. #5
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center