Saturday, December 29, 2007

Do you use the LoaderContext class?

Last night I spent several grueling hours trying to track down a bug in an Flash ActionScript 3 program I'm writing. I was getting an error when I would try to call a static method when the movie is loaded into another movie. I wouldn't get the error when I play the movie itself. The error was that the class name is an undeclared variable. Finally, I found a post on kirupa.com that led me to the answer.

The problem was relying on the default LoaderContext parameter to the Loader.load() method. You may not be familiar with this parameter, but it is important in how the Flash Player deals with an additional application being loaded in and how it prevents possible name collisions (see the thread I linked to above).

After solving the problem, I decided to look further into the books I have. Out of six books I looked in, all of them mentioned the Loader class but only one mentioned LoaderContext. Ironically, that was the last one I looked in because it is a Flex book, Programming Flex 2. It has a good explanation of the issues.

10 comments:

  1. I ran into the same problem. I was wondering if you could give me some help.. For the Images, I'm able to do Image loaderContext="{loaderContext}" and set loaderContext.checkPolicyFile to true. For VideoDisplay, I get the same error but there isn't a loaderContext property.

    ReplyDelete
  2. Can you give me more info on what is happening in your program? What value are you passing as the second argument to the call to load()? What error are you getting? What are you setting the applicationDomain to?

    ReplyDelete
  3. well for the images, I'm able to take a short cut. In flex, I can do [Image loaderContext="{loaderContext}" initialze="imageInit()"] inside init I can do loaderContext.setPolicyFile=true. But in VideoDisplay it doesn't come with loadContext property.. so I'm guessing I have to do var loader:Loader = new Loader();
    var flvLoaderContext:LoaderContext = new LoaderContext();
    flvLoaderContext.checkPolicyFile = true;
    loader.load(new URLRequest("http://remoteServer/test.flv"), flvLoaderContext);
    var bitmapData:BitmapData = new BitmapData (200, 200);
    bitmapData.draw (loader); not too sure how to draw the videoDisplay after that.. the error I get is SecurityError: Error #2122: Security sandbox violation. swf cannot access flv. A policy file is required, but the checkPolicyFile flag was not set where required.

    ReplyDelete
  4. Have you loaded the policy via:
    Security.loadPolicyFile("url");

    Did you look into specifying loaderContext.applicationDomain to something other than the default?

    ReplyDelete
  5. Security.loadPolicyFile("url"); worked!! thanks a lot! =]

    ReplyDelete
  6. john_r_nyquist YOU'RE THA MAN!!!

    thank you solved all of my problems!!! :D

    ReplyDelete
  7. Guys, check out the pdf at http://livedocs.adobe.com/flex/3/loading_applications.pdf, its a 48 page document describing application domain and loadercontext. Quite fascinating really, and helped me a ton getting my head around what appeared to be a black box issue.

    ReplyDelete
  8. thank you so much for Security.loadPolicyFile("url");

    tip. It works like charm for VideoDisplay in flex.

    You have to put url i.e. http://www..com into very first method of application Complete event.

    ReplyDelete
  9. one more thing added file name at the end in url
    http://www.XYZ.com/crossdomain.xml

    ReplyDelete