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 yourSWFLoaderobject, it should return the URL of the SWF and not use that as a parameter.public function random():String { // ... return url; }Loaderobject, maybe, for test purposes but you don't need that with anSWFLoaderobject.random()function) in your MXML code, you can use :<fx:Binding>tag :So your final code can be like this :
For more, take a look on data binding.
Hope that can help.