best way to embed swf that loads other swf files - html

In main.swf I loaded file.swf as:
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest(“file.swf”);
When main swf loaded in HTML with an absolute link like:
"http://domain.com/main.swf"
It reports 404 error loading failed of file.swf
Because it looks for file.swf at current web host.
What's the best way to do this without hard code absolute url in main.swf? Is this something swfobject can help?

"What's the best way to do this without hard code absolute url in main.swf?"
You could provide the main url thru flashvars or by making a PHP request
Is this something swfobject can help?
Yes if you use flashvars
var params = {/* whatever you need here*/}
swfobject.embedSWF("main.swf", "flash",
"100%", "100%", "9.0.0" ,
"expressInstall.swf" ,
{mainUrl:"http://example.com/"}, params );
In AS3
//In your main class
var mainURL:String = this.loaderInfo.parameters.mainUrl;
var loader:Loader = new Loader();
var url:String = mainURL + “file.swf”;
var request:URLRequest = new URLRequest(url);

Related

AS3 URLRequest not working in browser

My URL request is working perfectly in Flash Pro, but when I test it in the Browser, the image doesn't work at all. It doesn't load. Is there a Publish Setting I need to be using or something?
Please help, it's pretty frustrating. Here's what it looks like when it's in the browser.
Relevant code:
public function startSlidingPuzzle() {
// blank spot is the bottom right
blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
// load the bitmap
loadBitmap("http://fc07.deviantart.net/fs71/f/2014/030/d/7/slidingimage_by_nooodisaster-d74et6t.jpg");
}
// get the bitmap from an external source
public function loadBitmap(bitmapFile:String) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var request:URLRequest = new URLRequest(bitmapFile);
loader.load(request);
}
// bitmap done loading, cut into pieces
public function loadingDone(event:Event):void {
// create new image to hold loaded bitmap
var image:Bitmap = Bitmap(event.target.loader.content);
pieceWidth = image.width/numPiecesHoriz;
pieceHeight = image.height/numPiecesVert;
trace(numPiecesHoriz)
// cut into puzzle pieces
makePuzzlePieces(image.bitmapData);
// shuffle them
shufflePuzzlePieces();
}
You have a security error:
SecurityError: Error #2122: Security sandbox violation: Loader.content: http://fc03.deviantart.net/fs71/f/2014/030/e/e/beyonce_slidingpuzzle___flash_diary_day_10_by_nooodisaster-d74er8h.swf cannot access http://i.stack.imgur.com/ls9QI.jpg. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
at flash.display::Loader/get content()
at SlidingPuzzle/loadingDone()
Try adding a security context:
public function loadBitmap(bitmapFile:String)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
context.checkPolicyFile = true;
var request:URLRequest = new URLRequest( bitmapFile );
loader.load( request, context );
}
If that doesn't fix it you'll need to add a crossdomain.xml to the server hosting the images you are requesting. Why not save the images locally and deploy them in the same scope as your build?
[1]:

Accessing objects and variables from swf across domains

I am trying to access a variable from a test.swf on b.com from a parent.swf on a.com.
test.swf:
Security.allowDomain("a.com");
var test = 0;
parent.swf:
var loader:Loader = new Loader();
var url:String = "http://b.com/test.swf";
loader.load(new URLRequest(url));
I can access the swf like this. But how can I access and change the var test in test.swf?
Basically can do so by modifying the url of the swf you are loading in.
Check this out

Image path issue on as3

I'm trying to create a screensaver for one our of our display we have at work. Images will be uploaded to an external server, from that server I will have pull the images and xml file. so my flash app and my content will be in two different places. I'm getting an error "SecurityError: Error #2000: No active security context". how do I override error and get the images to my stage.
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML;
var imageList:XMLList;
var imageLoader:Loader = new Loader();
var timer:Timer =new Timer(5000);
var imageIndex:uint = 0;
var child:DisplayObject;
var path:String="http://bgxserv03.mgmmirage.org/interactivemedia/mmhub01/test/mb/edit_bay/hr/infoscreen/servamb/";
xmlLoader.load(new URLRequest(path +"output.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
timer.addEventListener(TimerEvent.TIMER, tick);
function xmlLoaded(e:Event) {
xmlData = new XML ( e.target.data);
imageList = xmlData.image.name;
timer.start();
loadImage(imageList[0]);
}
function imageLoaded(e:Event){
if (child){
myImageHolder.removeChild(child);
}
child = myImageHolder.addChild(imageLoader);
Tweener.addTween(child, {alpha:0, time:1, delay:4});
Tweener.addTween(child, {alpha:1, time:1, delay:5});
}
function loadImage(path:String){
imageLoader.load(new URLRequest( path +"photos/"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Any help would be deeply appreciate. Thank you.
You need to put the "Crossdomain.XML" on the server's root directory. This will allow your flash file to access the data (image in your case) from that server. You can get a sample xml from the following URL, customize it for your server:
Sample CrossDomain.XML
What you are missing is probably a crossdomain.xml policy file at the domain of your image/xml files.
Use this link to create a crossdomain.xml file and add it to the root of your image/xml domain like so : "http://bgxserv03.mgmmirage.org/crossdomain.xml"
The URLLoader load() function automatically checks for the crossdomain.xml. Loader class requires you specify that you are interested in checking for a policy file in a LoaderContext object sent to the load() function.
In your code, it looks like the error should be coming from the URLLoader xml file request, since it doesn't look like you are trying to access the bitmap data of your images in any way, which is normally what would throw a security error for image files. If it is a problem with the image loading part, then complete the following instructions and you should be set to go:
In your loadImage function, add a LoaderContext parameter to your load method call:
function loadImage(path:String){
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.checkPolicyFile = true;
imageLoader.load(new URLRequest( path +"photos/"), loaderContext);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Check out the spec for more info on how to use the Loader class.
If you run into any trouble, this thread may be helpful.

loading images and video in AIR without HTTP

I'm curious what the correct methodology is for loading image and video data directly from the file system, without employing HTTP.
I'm writing an AIR slideshow application, and it works great but currently relies on a local MAMP server to hand the app all the media via the standard, tried and true, FLASH media loading methodologies.
I know that since FLASH was developed as a web plugin it handles this way of receiving data best, but I'd really like to extricate this rather onerous and unnecessary bit and have the app as a stand-alone player, however, I'm unclear what the "correct" way is to load the media.
I have the File objects ready and I've gotten as far as having the user select the local directory from which to pull the media and I'm getting a list of the files (based on their extensions) from the list... but now what?
You first have to put the content of your file in a ByteArray (code from FileStream documentation)
var bytes:ByteArray = new ByteArray();
var myFileStream:FileStream = new FileStream();
var myFile:File = File.documentsDirectory.resolvePath("test.jpg");
myFileStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
myFileStream.openAsync(myFile, FileMode.READ);
function progressHandler(event:ProgressEvent):void
{
if (myFileStream.bytesAvailable)
{
myFileStream.readBytes(bytes, myFileStream.position, myFileStream.bytesAvailable);
}
else
{
myFileStream.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
loadImage();
}
}
Then you may load these bytes in a Loader to display the image (see method Loader.loadBytes)
function loadImage():void
{
var loader:Loader = new Loader();
loader.loadBytes(bytes);
addChild(loader);
}
Cheers
With this code the Loader fires the Event.COMPLTE event and you will be able to manipulate the Bitmap from it:
var bytes:ByteArray = new ByteArray();
var myFileStream:FileStream = new FileStream();
var myFile:File = File.documentsDirectory.resolvePath(YOUR_PATH);
myFileStream.addEventListener(Event.COMPLETE, fileCompleteHandler)
myFileStream.openAsync(myFile, FileMode.READ);
function fileCompleteHandler(event:Event):void
{
myFileStream.readBytes(bytes);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
loader.loadBytes(bytes);
function imageLoaded(e:Event):void
{
addChild(Bitmap(loader.content));
myFileStream.removeEventListener(Event.COMPLETE, fileCompleteHandler);
myFileStream.close();
}
}
PS: Thanks Kodiak, I made my code based on yours.

Action Script 3, Graph API - Load Facebook avatar, then use JPGEncoder to save image

im trying to load my facebook display image and then save it through as3 using JPGEncoder.
Right now im getting the image url and through a Loader, adding the image to a movie clip called foto1. This is working fine:
var image:URLRequest = new URLRequest("http://graph.facebook.com/" + response[0].id + "/picture?type=large");
imageLoader.load(
foto1.addChild(imageLoader);
The problem is when i try this:
var myImage:Bitmap;
var jpgSource:BitmapData = new BitmapData (foto1.width, foto1.height);
jpgSource.draw(foto1);
var jpgEncoder:JPGEncoder = new JPGEncoder(n);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
//Make sure to use the correct path to jpg_encoder_download.php
var jpgURLRequest:URLRequest = new URLRequest ("http://www.thewebchi.mp/pruebas_graph/download.php?name=" + fileName + ".jpg");
jpgURLRequest.requestHeaders.push(header);
jpgURLRequest.method = URLRequestMethod.POST;
jpgURLRequest.data = jpgStream;
var loader:URLLoader = new URLLoader();
navigateToURL(jpgURLRequest, "_blank");
If i try the jpg process on movie clip 'foto1' without loading the external image, it works fine.
When i load the image to 'foto1' the jpg process doesnt respond.
Thank you very much!!!
Is it necessary to use navigateToURL (opening in the browser window)? If not, I would suggest using the URLLoader, loader.load(jpgURLRequest). Also, using this approach you can listen for IOErrorEvent.IO_ERROR on the loader (and other events) that may give you some hints on what is wrong.