Flex: Get self SWF file name?

风格不统一 提交于 2020-01-14 08:46:50

问题


Is there a way I can programmatically determine the filename of the .swf my class is running in?

Thanks!


回答1:


Stage has a loaderInfo property, which contains a url property that has the information you're looking for. You can get the stage property from any DisplayObject in Flex.

trace(stage.loaderInfo.url);




回答2:


Just a helpful note: If you load one SWF into another, the loaded (inner) SWF will return an erroneous result if you use loaderInfo.url to try to get the filename. For instance, something like:

Path/To/Outer.swf/[[DYNAMIC]]/1

Instead of:

Path/To/Inner.swf

Beware!

That said, here is the code I use to get the current SWF name:

function SWFName(symbol:DisplayObject):String
{
    var swfName:String;
    swfName = symbol.loaderInfo.url;
    swfName = swfName.slice(swfName.lastIndexOf("/") + 1); // Extract the filename from the url
    swfName = swfName.slice(0, -4); // Remove the ".swf" file extension
    swfName = new URLVariables("path=" + swfName).path; // this is a hack to decode URL-encoded values
    return swfName;
}



回答3:


Not from within flash, afaik. What do you need it for? There might be a better way to do it.




回答4:


You can use loaderInfo.loaderURL to get the full path and name of you swf

Example of a class:

public class Main extends Sprite {
 private function init():void {
  removeEventListener(Event.COMPLETE, init);
  var myUrl:String=loaderInfo.loaderURL;
  var tmp:Array=myUrl.split("/");
  var myName:String=tmp[tmp.length-1].split(".swf")[0];
 }

 public function Main() {
  super();
  if (stage)
    init();
  else
    addEventListener(Event.COMPLETE, init, false, 0, true);
 }
}



回答5:


Things have changed a bit in more recent versions so I'll give an answer for Adobe Flash Builder 4.6 (geared towards Flash in browser, but you get the idea).

<s:Application ... applicationComplete="alertSwfUrl()">

<fx:Script>
<![CDATA[
    import mx.core.FlexGlobals;

    private function alertSwfUrl():void {
        var a:LoaderInfo = FlexGlobals.topLevelApplication.stage.loaderInfo;
        ExternalInterface.call('alert', a.url); 
    }

]]>
</fx:Script>

</s:Application

Check out the LoaderInfo docs to figure out how to use the loaderInfo object associated with the stage.



来源:https://stackoverflow.com/questions/1949059/flex-get-self-swf-file-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!