Archive

Posts Tagged ‘runtime sharing’

Runtime Sharing problems solved for good!

March 25th, 2008 1 comment

I stayed at work until 8:00 pm today because I could not figure out why I was getting a #1065 Reference Error when using Runtime Sharing.

The scenario

A main ActionScript application loads multiple swf files (screens). These swf import various symbols from a central swf (global_asset_library). When I load the first screen, I can see in ServiceCapture that the global_assets_library is requested, because the screen imports assets from it. When I load a second screen that should import assets from the same library, it does not request it, probably because it’s cached. It however displays a #1065 error and leaves a blank where I should have seen my beautiful scenery. So I could not load any subsequent screens because they use Runtime Sharing.

What happened internally?

From good old-fashioned trial-and-error and some incomplete documentation, I figured out that each swf, by default, loads the global_asset_library in its own context (security domain). That means that any other screen is now prohibited from accessing whatever was loaded by the first screen. Our global_asset_library (and I stress Global) now became the sole property of the first screen. Not what we want.

What do we do now?

We use a nice little class called LoaderContext from the flash.system package (AS projects don’t need to be converted to Flex – yay!). This LoaderContext will allow us to load all swf into the application, instead of the swf that requests it.

Show us the code!

var request:URLRequest = new URLRequest(swfPath);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
var loader:Loader = new Loader();
loader.load(request,context);

AS3 and Runtime Sharing

January 25th, 2008 No comments

Runtime Sharing has been quite a puzzle for me for some time now. Here are a few problems that I solved:

Exporting an imported asset
When in AS3 you want to import an asset from a swf, you get it by its linkage name. What if this asset needs to be imported from another swf? Flash only allows you to import OR export an asset for Runtime Sharing. Simple: in Flash, encapsulate the imported asset in a MovieClip, and export the parent clip with a different linkage name.

Load swf with runtime sharing dependencies
If you use the Loader class to load assets into your AS3 app and the source swf has dependencies, the Loader might not fire a COMPLETE event. Although you expect your app to go look for dependencies in the path relative to your source swf, it actually looks for it relative to the app. Since it doesn’t find it, your app has unpredictable behavior. Solution: move shared.swf to root, make an alias in root or change the linkage path in myassets.swf (might be tiresome).

Here is an example:

  • main.swf
  •  

  • assets
    • myassets.swf
    •  

    • shared.swf
  •  

     

myapp.swf is an AS3 app that loads myassets.swf
myassets.swf imports assets from shared.swf using the path “./shared.swf”
myapp.swf will look for dependencies in “root/assets.swf”, because that is the folder that contains the app.

Solution:

  • main.swf
  •  

  • shared.swf
  •  

  • assets
    • myassets.swf
  •  

     

It took me a long time and a bit of luck to figure it out. Hopefully you will benefit from it.