Accessing objects and variables from swf across domains - actionscript-3

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

Related

as3 loaded swf accessing variables

I have some questions with sharing/using/accessing variables/functions between loaded swf files.
my prj consists of main.swf file and 2 swf's which I load on first init of the main.swf.
my questions are:
1.how can I use variables from 1.swf in 2.swf (function is running in 2.swf)
2.how can I call a function from 2.swf in 1.swf
here is the code I'm using to load the swf's:
var playerMc:MovieClip = new MovieClip();
var dbMc:MovieClip = new MovieClip();
var m2Loader:Loader = new Loader();
var mLoader:Loader = new Loader();
startLoad();
function startLoad()
{
//var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("./_player/player.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc);
mLoader.load(mRequest);
addChild(mLoader);
//var m2Loader:Loader = new Loader();
var m2Request = new URLRequest("./_db/db.swf");
m2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc2);
m2Loader.load(m2Request);
addChild(m2Loader);
}
function loadMc(event:Event):void
{
if (! event.target)
{
return;
}
playerMc = event.target.content as MovieClip;
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc);
}
function loadMc2(event:Event):void
{
if (! event.target)
{
return;
}
dbMc = event.target.content as MovieClip;
dbMc.x = -400;
m2Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc2);
}
You have to stick with application domain.
In most cases you should load another swf in another application domain, but it's not really related to your question.
From loader, you must access to applicationDomain and then getDefinition. From there, you can get classes and use them in your main swf. Yes, you can read static properties.
If you need instances you should access loader#content. It is pointing to a root of loaded SWF. Root of loaded is SWF – is the instance of main class of the loaded swf.
Create a variable with no definition such as
public var MyClass;
as you can see i didnt add
public var MyClass:Class;
then in another function write
this.MyClass = this.mLoader.contentLoaderInfo.applicationDomain.getDefinition("NameOfClass") as Class;
i dont know much about this myself.. im having problems figuring out if you can only access Public static variables or if its possible to access normal public variables and possibly private variables because it is creating a new instance of the same class or however you want to word it..?
also after your write the above code .. when you want to change a varaibles this usually works for me
this.MyClass.RandomVariableName = this.MyClass.RandomVariableName + 1;
something like that..

AS3 can not load a Bitmap by using loadByte in Thread?

Flash Player supported Thread in 11.5+.
I want to load an image by using Loader.loadBytes() in Worker Thread.
Which the Image ByteArray are generated in Main Thread.
But I can NOT do it.
I got a SecurityError like this:
SecurityError: Error #2123: Security sandbox violation:
Loader.content: file:///E:/work/ASWorkSpace/test/bin-debug/test.swf
cannot access
file:///E:/work/ASWorkSpace/test/bin-debug/test.swf/[[DYNAMIC]]/1. No
policy files granted access.
I init my worker thread like:
worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
so it's not a swf from remote, but it's local or like
[Embed(source="../workerswfs/Thread.swf", mimeType="application/octet-stream")]
I found in manual.
It said "If the loaded content is an image, its data cannot be accessed by a SWF file outside of the security sandbox, unless the domain of that SWF file was included in a URL policy file at the origin domain of the image."
I have gotten a solution. I loadByte() twice, it seems "washed off" the ByteArray source. so FlashPlayer considered the ByteArray generated by Worker Thread, and it allowed to access loader.content. Like this:
//Messages to the Main thread
protected function onMainToWorker(event:Event):void {
var msg:ByteArray = mainToWorker.receive() as ByteArray;
trace(msg.length);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bytesComplete);
var bytes:ByteArray = new ByteArray();
bytes.writeBytes(msg,0,msg.length);
loader.loadBytes(bytes);
}
private function loadedAagin(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
// Here we can access it.
var bmp:Bitmap = loader.content as Bitmap;
}
private function bytesComplete(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, bytesComplete);
// var bmp:Bitmap = loader.content as Bitmap; It can not be accessed
// just loadBytes again, it seems "washed off" the ByteArray source
var newloader:Loader = new Loader();
newloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedAagin);
newloader.loadBytes(loader.contentLoaderInfo.bytes);
}
But it's so ugly isn't it?
Is there anyone has good idea?

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.

Access swf component from another swf file

Greeting,
I have two flash files myVideo1.swf and myVideo2.swf
Each one contents FLVPLAYBack component .
myVideo1.swf FLVPLAYBack component instance name is “video1”
myVideo2.swf FLVPLAYBack component instance name is “video2”
I want to be able to access myVideo2.swf from myVideo1.swf using as3.
I want when I click play button on myVideo2.swf the myVideo1.swf would stopped.
Bother flash files are in the same folder.
Please advice how to do this in action script 3
Regards,
First when you load your swf you need to set the loader context domain to your current domain
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
var request:URLRequest = new URLRequest('swf/assets.swf');
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loader.load(request, context);
Then you can access library items set to export using:
var MyClass:Class = Class(ApplicationDomain.currentDomain.getDefinition("export_id"));
var myInstance:Sprite = new MyClass() as Sprite;

best way to embed swf that loads other swf files

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);