Related Posts Plugin for WordPress, Blogger...
This form does not yet contain any fields.

    Follow Me on Pinterest

    Our products are on iTunes!

     Nanaimo Studio

    Find us on Google+ 

     

    We are listed on: Dmegs Link Directory

    Blog Index
    404 page

    Entries in global function (1)

    Tuesday
    Nov042008

    The Power of Global Reach

    Oftentimes during a development, you might find a need to implement your own custom global function to replace the ones provided by Flash player (such as trace( )).  For example, perhaps you need to output the classname with each trace call, or the time or frame number when the trace was called (see code below).

    Since there is no _global scope in AS3, the one mechanism for you to create global function is no longer available.  Fortunately in AS3, you can create your function in a package like you would with a class. There is a problem with this approach though - you would have to import the function every time you intend to use the function in a class, and you have more than one global functions, it can be a real pain to have to include them all. One way to get around this would be to turn them all into a static class methods, e.g. Debug.trace( ). With this approach, you can reduce the number of imports, but you can't away with no importing at all.

    Before you give up all hope, unnamed package comes to the rescue.  Unnamed package is basically where the folder where your FLA is located.  If you create your function in the unnamed package, it's visible to all classes within your application.

    package  
    {
        import flash.utils.getQualifiedClassName;
    
        public function AppTrace(msg:String, obj:*=null)
        {
    	var detailedMsg:String = new Date().toTimeString();
    	if (obj != null)
    	{
    	    var className:String = "";
    	    if (obj is String)
    	    {
                    className = obj;
    	    }
    	    else
    	    {
    		className = getQualifiedClassName(obj);
    	    }
    		
    	    detailedMsg += " [" + className + "] " + msg;
    	    trace(detailedMsg);
    	}
    	else
    	{
    	    detailedMsg += msg;
    	    trace(detailedMsg);
    	}
        }
    }