I want to create a view in flex that will display a randomly generated swf. the following code can run, but my swf isnt showing? how to fix this?
<fx:Script>
<![CDATA[
public function random(url:String):String{
var movieArray:Array = ['swf/maily_you', 'swf/maily_work', 'swf/maily_start'];
var loader:Loader = new Loader();
var index:int = movieArray.length * Math.random();
var url:String = movieArray[index] + '.swf';
trace("Attempting to load", url);
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOError);
addChild(loader);
function loaderComplete(e:Event):void {
trace("Successfully loaded", url);
} function loaderIOError(e:IOErrorEvent):void {
trace("Failed to load", url);
}
]]>
</fx:Script>
<s:Panel width="100%" height="100%">
<mx:SWFLoader width="480" height="320" id="loader1" source="random()"/>
</s:Panel>
In your posted code you have some little errors :
random()
function to set the source of your SWFLoader
object, it should return the URL of the SWF and not use that as a parameter.public function random():String { // ... return url; }
Loader
object, maybe, for test purposes but you don't need that with an SWFLoader
object.random()
function) in your MXML code, you can use :
<mx:SWFLoader width="480" height="320" id="loader1" source="{random()}"/>
<fx:Binding>
tag :
<fx:Binding
source="random()"
destination="loader1.source"
/>
<mx:SWFLoader width="480" height="320" id="loader1" source=""/>
So your final code can be like this :
<fx:Script>
<![CDATA[
public function random():String
{
var movieArray:Array = ['swf/maily_you', 'swf/maily_work', 'swf/maily_start'];
var loader:Loader = new Loader();
var index:int = movieArray.length * Math.random();
var url:String = movieArray[index] + '.swf';
return url;
}
]]>
</fx:Script>
<s:Panel width="100%" height="100%">
<mx:SWFLoader width="480" height="320" id="loader1" source="random()"/>
</s:Panel>
For more, take a look on data binding.
Hope that can help.