I have a Flex 3 app (player v9) which loads a Flash SWF (AS3, also player v9) and needs to dynamically pass it a collection of parameters which are known at run-time. These are parameters that are normally passed via the flashvars element in an HTML page. The embedded movie accesses these parameters via the loaderInfo.parameters object.
I've tried using SWFLoader and Loader classes with no success in param-passing.
Relevant details:
It's a local program, and cannot rely on query string parameters.
I've mucked with setting loaderInfo.parameters["foo"] = "123" from the embedding code, but the parameter never seems to wind up in the embedded movie.
I cannot place extra parameter-passing machinery in the embedded movie(s), as they are created by third parties.
Passing this params in URL won't help, because they're taken using javascript code in the html-wrapper.
The 'flashVars' params are taken using the Application.application.parameters, so, you have to set these params manually in your case.
If you are using SWFLoader to load another app, you should create the object, that will represent the application loaded and apply all you need:
<mx:Script>
<![CDATA[
import mx.managers.SystemManager;
import mx.controls.Alert;
import mx.events.FlexEvent;
private var loadedApp:Application;
private function onLoadComplete(event:Event):void {
var smAppLoaded:SystemManager = SystemManager(event.target.content);
smAppLoaded.addEventListener(FlexEvent.APPLICATION_COMPLETE, onLoadedAppComplete);
}
private function onLoadedAppComplete(event:FlexEvent):void {
try {
loadedApp = Application(event.target.application);
if(!loadedApp) throw new Error();
loadedApp.parameters["param1"] = "value1";
} catch (e:Error) {
Alert.show("Failed to get application loaded.", "Error", Alert.OK);
}
}
private function onLoadError():void {
Alert.show("Failed to load an application.", "Error", Alert.OK);
}
]]>
</mx:Script>
<mx:SWFLoader
width="100%" height="100%"
source="./AppToLoad.swf"
complete="onLoadComplete(event)"
ioError="onLoadError()" securityError="onLoadError()" />
The reason is simple.
I've discovered this today.
In the component loaded via SWFloader has parentApplication or Aplication.application set to the top level application (this witch loads component via SWFLoader).
And the loaded component can see flashvars set to the top level application.
This is probably the cause that setting parameters in SWFLoader does not have any impact.
I've set proper flashvars on my toplevel application and they are also seen in the loaded one :-).
When embedding a SWF on a web page you can pass flashvars as parameters on the URL to the SWF, perhaps the same could work in your case? If the SWF is located at file:///some/path/to/a.swf try using file:///some/path/to/a.swf?hello=world&foo=bar. It might work.
Would have saved myself a lot of time today if I had found this answer first: AS3 Pass FlashVars to loaded swf.
Essentially: since Flash Player 10.2 it has been possible to pass flashvars along by setting them as parameters on the LoaderContext.
Related
In Flex 3 I have a SWFLoader:
<mx:SWFLoader id="player" source="http://youtube.com/v/..." />
and after some time I invoke player.unloadAndStop(). And I always get this error:
ReferenceError: Error #1056: Cannot create property __tweenLite_mc on _swftest_mx_managers_SystemManager.
What does it mean and how to avoid this?
UPD: AIR 2 doesn't have this problem
Maybe try the Loader class? I'm not sure if it will help but I do all my loading via ActionScript. Generally speaking, I do "heavyWeight" programming/logic/cotrol stuff in ActionScript and leave Flex for more simplistic layout code. That is, flex puts things in place and actionscript controls it all. When loading clips in our Flex 3 project, I have control code along the lines of:
import flash.display.Loader;
private var loader:Loader;
public function init() {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadFailed);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleted);
}
with calls to things like:
//here, pop returns a string like "/path/to/movie.swf"
loader.load(new URLRequest(clipsToPlay.pop()));
...
loader.unload();
contained in functions like:
private function loadNextClip():void {
if(clipsToPlay.length == 0) {
dispatchEvent(new PlayBackCompleteEvent(PlaybackCompleteEvent.ALL));
return;
}
loader.load(new URLRequest(clipsToPlay.pop()));
}
private function loadCompleted(event:Event):void {
currentClip = event.target.content as MovieClip;
loader.unload();
displayClip();
}
private function displayClip():void {
applyEffects();
currentClip.addEventListener(Event.ENTER_FRAME, monitorForCompletion);
addChild(currentClip);
}
I'm not sure if Loader can be used instead of SWFLoader but if so I hope that helps you or someone else, in some way...
EDIT:
I just looked it up and mx.controls.SWFLoader and flash.display.Loader have very similar functionality. I'd try using Loader, as prescribed above, and see if it fixes the problem. You could probably initialize the loader via MXML, too, but I wouldn't recommend it since it's not a visual component, I think it's better to let MXML handle visual things while ActionScript handles logical things.
I have several mxml components in an app, all of which need the same variable called genericX. I've included that variable in the main mxml and made it public
[Bindable] public var genericX:Number = 102;
but I still can't access it from other mxml components. If I try to do this for example, it doesn't recognize the variable.
<s:Button x="{genericX}" label="Click" />
There's also a filthy solution that works but isn't nice. You can create a static variable against the application class. For example:
[Bindable] public static var genericX : Object
You can access that from anywhere like this:
MyApplicationName.genericX
It ain't pretty, but it does work :)
simon
You cannot access in this way. There is something called Events in Flex and you need to pass this variable in a MXML file to another using eventDispatcher.
For example
[Bindable] public var genericX:Number = 102;
private function init():void {
var evt:NewCustomEvent = new CustomEvent(CustomEvent.SENDDATA);
evt.genericaValue = genericX
dispatchEvent(evt);
}
Now you need to get into the MXML component where you want to recieve this Event and using addEventListner() to recieve this event and the corresponding variable.
Then finally Inject it into your button.
You should be able to access any global variables with:
Flex 3:
var app:Application = mx.core.Application.application as Application;
Flex 4(looks like what you're using):
var app:Object = FlexGlobals.topLevelApplication;
And then:
<s:Button x="{app.genericX}" label="Click" />
x="{parentApplication.genericX}"
Here is an example for sharing variables between MXML components by declaring them public in the main application.
Problem: An XML configuration file needs to be loaded at runtime and be ready when the application's createChildren() gets called. At the latest, because configuration values are needed to properly initialize child components. Preferably, I would like the configuration loading be complete before the application even gets created. In short, I want to do this:
load configuration, then
initialize the application using the loaded configuration.
I created a custom preloader to help solve this. But as it turns out, the application's createChildren() method already gets called during preloading, when the configuration is not yet guaranteed to be loaded. That is, before the custom preloader dispatches the COMPLETE event.
Thanks for any help in advance.
I found a solution to the problem. The key was to catch the preloader's FlexEvent.INIT_PROGRESS event, queue it, and stop its propagation until the configuration is fully loaded. This effectively stops the framework to proceed with application initialization. After the configuration loads, redispatch the queued events, letting the framework finish the preloading phase. Example code below (only relevant pieces):
public class PreloaderDisplay extends Sprite implements IPreloaderDisplay {
// mx.preloaders.IPreloaderDisplay interface
public function set preloader(preloader:Sprite):void {
// max priority to ensure we catch this event first
preloader.addEventListener(FlexEvent.INIT_PROGRESS, onInitProgress, false, int.MAX_VALUE);
startLoadingConfiguration();
}
private function onInitProgress(e:FlexEvent):void {
if (isConfigurationLoading) {
queuePreloaderEvent(e);
e.stopImmediatePropagation();
}
}
private function onConfigurationLoaded():void {
dispatchQueuedPreloaderEvents();
}
}
To use it in the application:
<mx:Application preloader="the.package.of.PreloaderDisplay">
The simplest way (I think) is to create a 'holder' canvas that will create the applications content after the context file is loaded, ie:
(psuedo code)
Application.mxml:
<mx:Canvas>
<mx:Script>
public function init():void{
loadXML();
}
public function handleXMLLoaded():void{
this.addChild(myApplicationContent);
}
</mx:Script>
</mx:Canvas>
MyApplicationContent.mxml
<mx:Canvas>
<!-- contains all your components etc -->
</mx:Canvas>
Is there a way I can programmatically determine the filename of the .swf my class is running in?
Thanks!
Stage has a loaderInfo property, which contains a url property that has the information you're looking for. You can get the stage property from any DisplayObject in Flex.
trace(stage.loaderInfo.url);
Just a helpful note: If you load one SWF into another, the loaded (inner) SWF will return an erroneous result if you use loaderInfo.url to try to get the filename. For instance, something like:
Path/To/Outer.swf/[[DYNAMIC]]/1
Instead of:
Path/To/Inner.swf
Beware!
That said, here is the code I use to get the current SWF name:
function SWFName(symbol:DisplayObject):String
{
var swfName:String;
swfName = symbol.loaderInfo.url;
swfName = swfName.slice(swfName.lastIndexOf("/") + 1); // Extract the filename from the url
swfName = swfName.slice(0, -4); // Remove the ".swf" file extension
swfName = new URLVariables("path=" + swfName).path; // this is a hack to decode URL-encoded values
return swfName;
}
Not from within flash, afaik. What do you need it for? There might be a better way to do it.
You can use loaderInfo.loaderURL to get the full path and name of you swf
Example of a class:
public class Main extends Sprite {
private function init():void {
removeEventListener(Event.COMPLETE, init);
var myUrl:String=loaderInfo.loaderURL;
var tmp:Array=myUrl.split("/");
var myName:String=tmp[tmp.length-1].split(".swf")[0];
}
public function Main() {
super();
if (stage)
init();
else
addEventListener(Event.COMPLETE, init, false, 0, true);
}
}
Things have changed a bit in more recent versions so I'll give an answer for Adobe Flash Builder 4.6 (geared towards Flash in browser, but you get the idea).
<s:Application ... applicationComplete="alertSwfUrl()">
<fx:Script>
<![CDATA[
import mx.core.FlexGlobals;
private function alertSwfUrl():void {
var a:LoaderInfo = FlexGlobals.topLevelApplication.stage.loaderInfo;
ExternalInterface.call('alert', a.url);
}
]]>
</fx:Script>
</s:Application
Check out the LoaderInfo docs to figure out how to use the loaderInfo object associated with the stage.
I'm currently maintaining some flex code and noticed very many functions which are declared like:
private function exampleFunc():void {
....
}
These functions are in the global scope, and aren't part of any specific class, so it's a bit unclear to me what effect declaring them as private would have. What restrictions does the "private" qualifier have for functions like this?
The actionscript functions that are included in your mxmlc code will we available as a part of your mxmlc component, which behind the scenes is compiled into a class. Therefore marking them as private makes them inaccessible.
Here is an example to make that clear, say you have the following component, we'll call it FooBox:
<!-- FooBox.mxml -->
<mx:Box xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Script><![CDATA[
private function foo():void {
lbl.text = "foo";
}
public function bar():void {
lbl.text = "bar";
}
]]></mx:Sctipt>
<mx:Label id="lbl">
</mx:Box>
I can now add FooBox to my application, and use it's functions:
<mx:Application
xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns:cc="controls.*"
>
<mx:Script><![CDATA[
private function init():void {
fbox.foo(); // opps, this function is unaccessible.
fbox.bar(); // this is ok...
}
]]></mx:Sctipt>
<cc:FooBox id="fbox" />
</mx:Application>
If the actionscript functions are included in your Main Application file, the I think you can call the functions from an child control through the Application.application object, something like:
Application.application.bar();
if the bar function was placed in the main mxmlc code.
What do you mean by global scope? Are these functions declared in the main MXML file?
In general, private means that functions can only be called from within the class that declares them.
But, when you put it in a actionscript file .as is it still compliled into a class ?
Because asdoc doesn't like it.