AS3: My favorite helper functions


 / Published in: ActionScript 3
 

These are my favorite helper functions. getFlashVars() is used to quickly pull out variables from SWFObject2, getDevelopmentMode() is used to determine whether you are working locally on your machine or if the SWF is hosted on a server, getContextPath() is used to determine what directory the SWF is located

  1. public function SlideShow( ){
  2. //If you are working locally on your computer, you will load manually load the xml file. Otherwise you will be receiving the xml file from SWFObject2
  3. xmlURL = ( getDevelopmentMode() === "Production" ) ? getFlashVars().xml : "./xml/data.xml";
  4.  
  5. //Capture where the SWF is living on the live site
  6. var currentLocationOfSWF:String = getContextPath();
  7. }
  8.  
  9. private function getFlashVars():Object
  10. {
  11. return Object( LoaderInfo( this.loaderInfo ).parameters );
  12. }
  13.  
  14. private function getDevelopmentMode():String
  15. {
  16. var dev:Boolean = new RegExp("file://").test( this.loaderInfo.loaderURL);
  17.  
  18. if( dev ) return "Development";
  19. else return "Production";
  20. }
  21.  
  22. private function getContextPath():String
  23. {
  24. var uri:String = getLoaderURL();
  25. return uri.substring(0, uri.lastIndexOf("/")) + "/";
  26. }
  27.  
  28. private function getLoaderURL():String
  29. {
  30. return this.loaderInfo.loaderURL;
  31. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Winkyboy on June 29, 2010

Just a little note for myself, the next time I come back through here and need to pass flashvars through to a getURL in an .as file...

import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.net.navigateToURL;

    private function getURL(url:String, target:String = null){
        try {
            navigateToURL(new URLRequest(url), target);
        }catch(error:Error){
            trace("[getURL] "+error);
        }
    }

    private function getFlashVars():Object
    {
         return Object( LoaderInfo( this.loaderInfo ).parameters );
    }

// using info from Adobe to pass the flashvars: http://kb2.adobe.com/cps/164/tn_16417.html

Posted By: Winkyboy on June 29, 2010

Oops! And here's the call to get it going:

getURL (getFlashVars().clickTag, "_blank");

You need to login to post a comment.