pass a base path to a swf loaded at runtime - actionscript-3

I have a main.swf which loads a module.swf and the module.swf loads some assets.
The module.swf works standalone and also needs to work when loaded by main.swf.
But unfortunately the module.swf can't find the assets when loaded by main.swf because the assets aren't located relative to the main.swf, but are located relative to the module.swf.
As I can't touch the module.swf and I'm also unwilling to change the directory structure, I am looking for a solution close to the a "base" parameter which can be used when a swf is embedded into html.
Is there a way to simulate the base parameter's behaviour when loading a swf file using Loader?
Here is a similar yet unanswered question.

You could use symlinks as a way to redirect your assets url when loaded via the main swf

Something like this
public class URLUtil
{
public static function getBaseURL(swfURL:String):String
{
return swfURL.substr(0, swfURL.lastIndexOf("/") + 1);
}
}
example of usage inside module:
var assetURL:String = URLUtils.getBaseURL(root.loaderInfo.url) + "asset.filename";
loader.load(new URLRequest(assetURL));

Related

HTML from external file in MovieClip?

I have a client that is requesting an area be built on the stage of an existing .fla file (that I built as well) that loads html content from an external file. This area will serve as a dynamic ad banner that displays the contents of the external file. Ideally, the external file would contain links to different .html files that can easily be edited as the content needs change.
I have tried HTMLloader, but I am limited to a .swf file only and cannot use AIR.
Any thoughts on the best way to do this?
Thanks in advance!
If you are in fact needing to pull content from an external .html file, the following code showcases how to load external html content. In my example, I stored some content in a file named 'MyBannerAd.html', but you can point it to wherever you want and whatever filename you wish. Once the file has loaded, the externalFileLoadComplete function is called at which time I created a textfield and stored the contents of the file that was loaded from the external file as the htmlText property of the TextField. If you already have a textfield created, you can simply reference that one instead. The code shown below executed successfully using Flash CC 2014.
import flash.text.TextField;
function externalFileLoadComplete(e:Event):void
{
var myTextField:TextField = new TextField();
myTextField.htmlText = e.target.data;
addChild(myTextField);
}
var fileLoader:URLLoader = new URLLoader();
var fileRequest:URLRequest = new URLRequest("MyBannerAd.html");
fileLoader.addEventListener(Event.COMPLETE, externalFileLoadComplete);
fileLoader.load(fileRequest);

How to avoid hardcoding full URL of an xml file?

I am bound to provide a full URL : www.abc.com/folder1/folder2/my.xml rathar than just giving relative URL "./my.xml" .
How can i solve this ? This forces me to hardcode the URL inside the .as file .
I can't understand what you actually need: relative or absolute URL?
If relative, use it. Just consider that it should be relevant to the HTML wrapper, not the SWF itself.
If absolute and you just do not want to embed it into the code, pass it to your SWF from the HTML wrapper using FlashVars.
You can use a relative url in the flash, and the fact that it's working when you test locally indicates a problem with the embed. My guess is that you have placed the swf in a subfolder from the html page but not specified a base url when embedding the swf. Is the swf file located in www.abc.com/folder1/folder2/ or any other folder other than the html itself?
If so, you need to tell the flash player which url all relative url:s in the flash should use as a starting point. This is done using the "base" parameter. If you embed the swf using SWFObject you specify it with an object with a base property:
var flashvars = {
};
var params = {
base: "/folder1/folder2/"
};
var attributes = {
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes);
Note that I assumed that the .swf is in folder2/ in the example above.
Hope this solves your problem.

firefox does not load an xml into an swf called by a parameter in the html-code (works fine in IE)

I built an excercise in flash cs5/as3. It draws its content from an xml-file.
Because I want to make it easy for other people to create their own excercise based on this swf, I want the actionscript to load the xml, with the filename based on a parameter in the html code.
In this case the xml is called oef01.xml
The link would look like this: BoFlitsOefening.swf?id=oef01
And the actionscript like this:
public function Main ()
{
//myFile is a string I declared earlier
myFile = LoaderInfo(this.root.loaderInfo).parameters["id"];
myFile += ".xml";
loadXml ();
}
function loadXml ():void
{
xmlLoader = new URLLoader(new URLRequest(bestand));
xmlLoader.addEventListener (Event.COMPLETE,xmlLoaded);
}
function xmlLoaded (event:Event):void
{
myList = new XML(event.target.data);
myList.ignoreWhite = true;
}
The construction is working fine in Internet Explorer but not in Firefox. I have done internet research but I could not find an explanation or a solution I was able to apply, though the issue is known.
Are you testing it on the server or locally - the URL in your browser should start with http:// not file:///?
It should work fine on the internet, while locally the URLs containing ? may not resolve properly.
In that case you may use FlashVars instead - you don't need to change the AS code, just HTML/JS.
And on a side note: you may try to embed the SWF file using SWFObject - some crossbrowsers issues are caused by wrong/buggy embedding code.
Also FireFox likes to keep external sources cached, so there's a likelihood that it's loading an old file. Make sure you clear the cache after updating the xml.
there is a way of tricking it to load it fresh every time by adding some junk after, like
.../my.xml?rand=001820018
where you generate the number randomly every time, if I remember correctly

How to add image in the library

I have the image path manipulate from database with php. Now how can i have to import those image in the library so that i can use them as a symbol. Thanks in advance.
You cannot load it into library, but you can load this image via php and use it on stage with action script. Take a look at this answer to the similar question:
Load image dynamicly in flash
You can download it to the server and generate source file with Class that have field like this:
class Library
{
[Embed(source = 'path_to_folder/image_name.png')] public static var imageClass : Class;
}
Alsou you should refer this class somewhere to be sure it will be compiled into your swf and use it like this:
const image:Bitmap = new (Library.imageClass)();
In that case you need to recompile your swf every time the image is changed.
But I'm totaly agree with Bartek that it is better to use dynamic loading.

Alternative for getDefinitionByName

In the past, there was a simple trick, to include the flex mxmlc module by adding the following line to the flash-cs4 makefile:
-include-libraries “/absolute/path/to/my/assets/assets.swc”
This gave you the ability to use getDefinitionByName, an helper function to access the embedded swc asset-library (instead of creating hand-written classes all assets).
Unfortunately, this has stopped working since flash-cs4. Does anybody know another solution?
Unfortunately, the only workaround I have found is to explicitly refer each of asset classes somewhere in the code. You may create dummy class like this:
public class AssetsExporter extends Sprite
{
public static function export()
{
AssetClass1;
AssetClass2;
//etc
trace( "DEBUG AssetsExporter.export()" );
}
}
Specify this class as Document class in Flash IDE, so it will be compiled into the resulting swc. Then in the main code of your application call
AssetsExporter.export();
After doing this you will be able to use getDefinitionByName().
You can add them to the libraries in the publish settings.
(Image from http://wiki.gigya.com/ via Google Images)
By the way, if you use SWC files, you can also do
new somepackage.SomeClass();
instead of
new getDefinitionByName("somepackage.SomeClass")
where applicable. This is because SWC files are included at compile time, not runtime.
Even though you can change a compiler setting manually, its easy if you are using FlashDevelop as this is very simple to fix.
Right click your included swc from the Project list. Choose options then "include library (complete library)".
..you can now use getDefinitionByName to get an unreferenced class from your swc file.