I am trying to load a swf file which contains xml based image gallery on 25th frame of the timeline inside flash professional cs5.5 using actionscript 3. while doing so i’m getting this error “SecurityError: Error #2000: No active security context”. Below is the as3 code:
stop();
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad() {
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("../xml_gallery_2_852/XMLGALLERY2.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent) {
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();
Security errors are thrown when an operation is not permitted by the security sandbox the application runs in. This can have several reasons – not the least of which would be trying to access a local file from an application that was compiled with
use-network=true.EDIT
Re-reading your question I have come across the most possible cause, but I’ll leave the other info in, anyway – it might be useful to other users.
You are loading an image gallery, which in turn loads other files (XML), but the external SWF was compiled with the
use-networkoption, while your FLA automatically runs aslocal-trustedapplication when exported from the Flash IDE. This is of course a sandbox violation.You can test this by starting your SWF from a web server that has a valid security policy – if I assume correctly, your app should work then.
By the way, if you add an event listener to the
contentLoaderInfoto handle security errors, you can prevent your app from crashing and exit gracefully.END EDIT
The error could be related to a short delay in between instantiation of the loader and initialization of its security context. See this blog post for details.
If this is true, you should be able to properly get rid of the error by delaying the load request using
setTimeout()or moving the declaration of your loader outside of the function block, i.e.:Declaring your loader as a temp variable is “dirty” anyway: The reference is lost, but the event listeners you’ve added to
contentLoaderInfokeep it alive in memory, regardless of whether it is still needed or not. This can lead to serious memory leaks, if you’re going to load more than one file. You should always keep a reference to your loader, if you want to properly dispose of loaded content when it is no longer needed (by usingLoader.unload()), and to make the loader itself available for garbage collection (by removing event listeners and explicitly setting the reference tonullafter use).