I'm trying to load in a remote SWF and access it's methods and properties, using an interface. (There's a similar question here that got as far as "that's weird!" but didn't resolve it: Loading swf and using it through interface)
My remote SWF looks like this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.system.Security;
import IMultiplayer;
[SWF(width="238", height="60", frameRate="60", backgroundColor="#FFFFFF")]
public class Main extends Sprite implements IMultiplayer
{
public function init(e:Event):void
{
}
public function TEST():void
{
trace("TEST()");
}
}
}
I then have an interface that looks like this:
package
{
import flash.events.*;
import flash.display.*;
public interface IMultiplayer
{
function init(e:Event):void;
function TEST():void;
}
}
And finally, I've got a loader class that pulls down the SWF and tries to cast it as the same interface that the remote SWF implements. EDIT - apologies for length, was asked to post the full source:
package uk.co.MyDomain
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.system.SecurityDomain;
import flash.utils.Timer;
import uk.co.MyDomain.*;
import utils.Console;
public class MultiplayerLoader extends Sprite
{
private var ld:Loader;
private var _environment:String;
public var _mpInstance:IMultiplayer;
private const SANDBOX_SWF:String = "http://static.sandbox.dev.MyDomain.co.uk/smartfoxtest/dev/swf/MP.swf";
public function MultiplayerLoader(environment:String)
{
_environment = environment;
}
public function loadMultiplayer():void
{
ld = new Loader();
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
ld.contentLoaderInfo.addEventListener(Event.COMPLETE, multiplayerLoaded);
ld.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onLoadError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOLoadError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.DISK_ERROR, onDiskError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onNetworkError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.VERIFY_ERROR, onVerifyError);
if(Security.sandboxType == Security.REMOTE)
{
trace('[MP Loader] Loading with context');
ld.load(new URLRequest(SANDBOX_SWF), context);
}
else
{
trace('[MP Loader] Loading with NO context');
ld.load(new URLRequest(SANDBOX_SWF));
}
}
private function onIOLoadError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onDiskError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onNetworkError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onVerifyError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onLoadError(e:ErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function multiplayerLoaded(e:Event):void
{
var tester:IMultiplayer = e.currentTarget.content as IMultiplayer;
Console.log('Loaded: ' + tester);
dispatchEvent(new MultiplayerEvent(MultiplayerEvent.SWF_LOAD_SUCCESS));
}
private function notifyFailure(reason:String):void
{
var failEvent:MultiplayerEvent = new MultiplayerEvent(MultiplayerEvent.SWF_LOAD_FAILURE);
failEvent.params = {'reason':reason}
dispatchEvent(failEvent);
}
}
}
Now, if I DON'T cast it to use the interface, I can trace it out successfully and call functions (so e.target.content.TEST() will fire). However, as soon as I cast it to the interface, it fails.
Any ideas?
EDIT
OK, I'm getting the same issue with a custom event class that's shared between both applications. Flash errors, saying it cannot convert from one class to the other - even though they're identical they're in different projects, and so I imagine different namespaces. I assumed that loading into the same applicationDomain would fix this, but it hasn't. Is there any other way I can get around this without resorting to a common library/SWC or similar?
I think this might be an issue of the application domain. The loaded SWF resides in its own application domain so it does not share the exact same interface. Try to load the swf into the »current application domain«, using the applicationDomainproperty of the Loader's LoaderInfo Object.
look here
good luck…
EDIT::
it has to be done in the loaders load method
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
var loader:Loader = new Loader();
loader.load(new URLRequest("myButtons.swf"), context);
from here
EDIT 2::
Once I ran into an even more strange error, it was caused by the fact that i created an interface, compiled the »to load files« and »file that loaded«, changed the interface through adding a method and forgot to compile the »to load files« (there were a lot). Perhaps happened something like this…
EDIT 3::
If I remember right than one has to wait for the Event.INIT event of loaded swf's, first after this event the constructor of the loaded swf ran.
EDIT 4::
found this, perhaps you need to do:
var YourInterfaceClass:Class = ApplicationDomain.currentDomain.getDefinition( "YourInterface" ) as Class;
var myInstance:YourINterface = event.target.content as YourInterface;
Related
I am using Flash Builder 4.7 and have created a Worker Class. Below is the code:
package co.fuix.mobile.system.model
{
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.utils.getDefinitionByName;
import mx.managers.SystemManager;
public class InstantMessengerWorker extends Sprite
{
private var bm:MessageChannel;
private var mb:MessageChannel;
public function InstantMessengerWorker()
{
super();
bm = Worker.current.getSharedProperty("BACK_TO_MAIN_CHANNEL");
mb = Worker.current.getSharedProperty("MAIN_TO_BACK_CHANNEL");
mb.addEventListener(Event.CHANNEL_MESSAGE, onMainToBack);
}
protected function onMainToBack(event:Event):void
{
if(mb.messageAvailable){
var s:SystemManager;
trace('*'+mb.receive());
trace('**'+mb.receive());
trace('***'+mb.receive());
trace(mx.core.FlexGlobals.topLevelApplication.myVariable);
}
}
}
}
How do I reference a variable in the main mxml file. I know how to use message channels but I want to get that variable straight.
When I run the above code, this part
trace(mx.core.FlexGlobals.topLevelApplication.myVariable);
is giving me an error.
Any help will be regreatly appreciated
You can't access a variable from the main app like that. They are running separately. What you need to do is:
Link to Adobe docs
This happens on many occasions when loading content from the web, but for us, it's most common loading images through Facebook's graph shortcut call.
Something as simple as:
package
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.LoaderContext;
public class RedirectTestFail extends Sprite
{
private const url:String = 'https://graph.facebook.com/4/picture';
private const context:LoaderContext = new LoaderContext(true);
public function RedirectTestFail()
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest(this.url), this.context);
}
protected function onComplete(event:Event):void
{
this.addChild((event.target as LoaderInfo).content);
}
}
}
Gives a dreaded "SecurityError: Error #2122" error.
Despite other answers suggesting something as simple as:
Security.loadPolicyFile("https://fbcdn-profile-a.akamaihd.net/crossdomain.xml");
This isn't clear, nor comprehensive enough. Facebook have different image servers, that I've which I've been caught with before. This could be deemed a Flash Player Bug, which I'd accept, but as a security concern, I can understand them not allowing a redirect by default, as in you should handle it yourself.
I now use below. You try to do your normal behaviour, but wrap it in a try/catch for SecurityError. If one is thrown, catch it, and if the domain of the loaderInfo is different to the domain you requested, you run a 'Security.allowDomain' and 'Security.loadPolicyFile' on it, and attempt to load it one more times. This works perfectly in practise, with only a tiny amount of overhead.
package
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.Security;
public class RedirectTest extends Sprite
{
private const url:String = 'https://graph.facebook.com/4/picture';
private const context:LoaderContext = new LoaderContext(true);
public function RedirectTest()
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest(this.url), this.context);
}
protected function onComplete(event:Event):void
{
try
{
this.addChild((event.target as LoaderInfo).content);
}
catch(error:SecurityError)
{
trace(error);
var loaderInfo:LoaderInfo = (event.target as LoaderInfo);
var loaderDomain:String = loaderInfo.loader.contentLoaderInfo.url;
if(-1 == this.url.indexOf(loaderDomain))
{
Security.loadPolicyFile(loaderDomain + 'crossdomain.xml');
if( 0 == loaderDomain.indexOf('https') )
{
Security.allowDomain(loaderDomain);
}
else
{
Security.allowInsecureDomain(loaderDomain)
}
loaderInfo.loader.load(new URLRequest(this.url), this.context);
return;
}
throw error;
}
}
}
}
if you no need to manipulate pixels with loaded image BitmapData object, then you can just remove context from the loader.load
but without context.checkPolicyFile = true you will cant add smoothing to image
I'm trying to get the hang of the AS3 workers, but there must be some elusive bit of understanding that just escapes me.
I've build a fairly simple PoC to see how it should work, but with no luck. When I run the "master" SWF, it seems to load the worker SWF fine and goes through everything without a hitch, except there's no response from the bloody worker.
I'm using Flash Builder 4.6 with FlexSDK 4.9.1, the PoC projects are built as ActionScript projects.
The worker file:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.system.MessageChannel;
import flash.system.Worker;
public class WorkerPOC extends Sprite
{
private var wToM:MessageChannel;
private var mToW:MessageChannel;
public function WorkerPOC()
{
wToM = Worker.current.getSharedProperty("wToM") as MessageChannel;
mToW = Worker.current.getSharedProperty("mToW") as MessageChannel;
trace(mToW.receive());
wToM.send("Ready");
}
}
}
The master file:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.system.WorkerDomain;
import flash.utils.ByteArray;
public class WorkerMaster extends Sprite
{
private var workerLoader:URLLoader;
private var workerData:ByteArray;
private var worker:Worker;
private var wToM:MessageChannel;
private var mToW:MessageChannel;
public function WorkerMaster()
{
workerLoader = new URLLoader();
workerLoader.dataFormat = URLLoaderDataFormat.BINARY;
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
workerLoader.addEventListener(Event.COMPLETE, onHasWorker);
workerLoader.load(new URLRequest("workers/WorkerPOC.swf"));
}
private function onHasWorker(event:Event):void
{
workerData = workerLoader.data as ByteArray;
workerData.shareable = true;
worker = WorkerDomain.current.createWorker(workerData);
wToM = worker.createMessageChannel(Worker.current);
wToM.addEventListener(Event.CHANNEL_MESSAGE, onMessage);
wToM.addEventListener(Event.CHANNEL_STATE, onState);
mToW = Worker.current.createMessageChannel(worker);
worker.setSharedProperty("wToM",wToM);
worker.setSharedProperty("mToW",mToW);
worker.start();
mToW.send(123);
}
private function onState(event:Event):void
{
trace("Channel state: ", wToM.state);
}
private function onMessage(event:Event):void
{
trace(wToM.receive());
}
}
}
I've been working with workers as well. I had them working for a while then everything kinda just stopped, in the same way yours is not working.
Looks like the messagechannel is not sending the messages properly in debug in 11.7.
Not sure why it is happening, but try running your code without the debugger attached when the worker is created. When I do that it works fine...
Sounds strange, but have you applied
-swf-verion=XXX // XXX must be > 17
as a compiler argument? I am asking this, because I had a very similar problem :)
Workers in Apache Flex 4.7
I am working on an Air 2.6 project with an embedded SWF.
I am using the following embed code:
[Embed(source = "../../assets/click_feedback.swf", symbol="sub_circle")]
[Bindable]
public static var click_feedback:Class;
And the following code to get an instance of the click_feedback class:
private var cfb:MovieClip = new Assets.click_feedback() as MovieClip
The problems are:
The asset sub_circle has a frame labeled 'respond'. However, it just plays non stop whether or not the label is called with gotoAndPlay.
And, at the end of the animation, there is an Event.COMPLETE called, which is not being picked-up by my code.
I have tested the sub_circle asset in CS5 where I built it, and, in that environment it does not animate until 'respond' is called, and the event it triggers can be heard by my script.
Is this the correct way to handle embedded assets from an SWF?
Embedding of separate symbols from swf in general isn't a good idea:
It slows the compilation of your project, because of compiler must transcode swf format.
Embedding delete ALL AS code from timeline (I didn't check, but may be it remove labels as well, and it can be the reason of your issue).
I recommend embed the hole swf file and loads its bytes in runtime, a little more code to handle the async loading, but much more freedom and flexibility in use:
you safe all as3 code
you can choose ApplicationDomain where to load classes
you can easily switching from Embedding to runtime swf loading by url at any time, if you want to separate the as code and art/sounds assets.
organize your assets to library with the AssetsManager (with api like load(ByteArray or URL), getSkin(name):DisplyObject)
Code example adapted for your assets:
package
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
[SWF(width="800", height="200", backgroundColor="0x8B8B8B")]
public class astest extends Sprite
{
[Embed(source="../../assets/click_feedback.swf", mimeType="application/octet-stream")]
private static const common_art:Class;
private var loader:Loader;
private var domain:ApplicationDomain = ApplicationDomain.currentDomain;
public function astest()
{
init();
}
public function init():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var loaderContext:LoaderContext = new LoaderContext(false, domain);
loaderContext.allowCodeImport = true;
loader.loadBytes(new common_art() as ByteArray, loaderContext);
}
private function onLoaded(event:Event):void
{
var clip:MovieClip = getSkin("sub_circle") as MovieClip;
addChild(clip);
}
private function getSkin(name:String):DisplayObject
{
if(domain.hasDefinition(name))
{
var clazz:Class = domain.getDefinition(name) as Class;
return new clazz() as DisplayObject;
}
return null;
}
}
}
I am just trying to make a simple .swf file that plays a piece of audio when it loads. This compiles but when I bring it up into the browser nothing happens. I could only find sprite based tutorials so I took a stab that you can extend Sound the same way as you would extend Sprite. The final version is going to be headless and called my Java Script to play audio on Events.
package {
import flash.media.Sound;
import flash.net.URLRequest;
public class typeRight extends Sound {
public function HelloWorld( ) {
load(new URLRequest('./sound.mp3'));
play();
}
}
}
I am NOT working in Flash so please no GUI advice ; )
Rather than subclassing the Sound class, create a document class like this that contains a Sound class in it:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class SoundPlayer extends Sprite
{
protected var _sound : Sound;
protected var _channel : SoundChannel;
public function SoundPlayer()
{
_sound = new Sound();
_sound.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
_sound.addEventListener(IOErrorEvent.IO_ERROR, loadError);
_sound.load(new URLRequest("./sound.mp3"));
}
protected function soundLoadCompleteHandler(evt : Event) : void
{
// Use the _channel object to control sound properties such as pan and volume.
_channel = _sound.play();
}
protected function loadError(evt : IOErrorEvent) : void
{
trace ("ERROR :: " + evt);
// You could try recovering from the error here.
}
}
}