Bug in Flex SDK?

There is a strange behavior in the Flex SDK (currently version: 3.4.0.7653) that I think is not intended. If you try to load a SWF file using the Loader class inside a class named Main the SWF is never instantiated. To recreate what I think have to be a bug do as follow:

Copy the class below into either FlashDevelop (with Flex SDK) or Flex, and compile it, and you will see that neither Event.INIT or Event.COMPLETE is dispatched. Then try to rename the file, class and constructor to whatever you want but “Main”, and you can see that the SWF is loaded and both Event.INIT and Event.COMPLETE is dispatched.

Update: As Philippe writes in a comment to this article there is a class called “Main” in the loaded SWF. It was a conflict, not a bug. ;-) I’ll leave the article as it is anyway as a reference for others. Here is an updated FlashDevelop project with the changes Philippe suggested:  LoadStuff.zip

Here is the FlashDevelop project I used to test this: loadstuff.zip

package<br />
{</p>
<p>	import flash.display.Loader;<br />
	import flash.display.Sprite;<br />
	import flash.events.*;<br />
	import flash.net.URLRequest;<br />
	import org.casalib.util.StageReference;</p>
<p>	/**<br />
	 * &#8230;<br />
	 * @author Thomas Gabrielsen<br />
	 */<br />
	public class Main extends Sprite<br />
	{</p>
<p>		private var _loader:Loader;<br />
		private var _container:Sprite;</p>
<p>		public function Main():void<br />
		{</p>
<p>			if ( stage ) init();<br />
			else addEventListener( Event.ADDED_TO_STAGE, init );</p>
<p>		}</p>
<p>		private function init( e:Event = null ):void<br />
		{</p>
<p>			removeEventListener( Event.ADDED_TO_STAGE, init );</p>
<p>			var req:URLRequest = new URLRequest( &quot;assets/SideMenu.swf&quot; );<br />
			_loader = new Loader();<br />
			_loader.load( req );</p>
<p>			_loader.contentLoaderInfo.addEventListener(Event.INIT, loadInitHandler);<br />
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);<br />
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);<br />
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgressHandler);</p>
<p>		}</p>
<p>		private function loadInitHandler( event:Event ):void<br />
		{</p>
<p>			trace( &quot;==============\r THE LOADED SWF IS INSTANTIATED \r==============&quot; );</p>
<p>		}<br />
		private function loadCompleteHandler( event:Event ):void<br />
		{</p>
<p>			trace( &quot;==============\r LOADING IS COMPLETE \r==============&quot; );<br />
			addChild( _loader );</p>
<p>		} </p>
<p>		private function errorHandler( event:IOErrorEvent ):void<br />
		{</p>
<p>			trace( &quot;==============\r IO ERROR \r==============&quot; );</p>
<p>		}<br />
		private function loadProgressHandler( event:ProgressEvent ):void<br />
		{</p>
<p>			var percent:Number = event.bytesLoaded / event.bytesTotal * 100;<br />
			if (percent == 100) trace( &quot;==============\r 100% LOADED \r==============&quot; );</p>
<p>		}</p>
<p>	}	</p>
<p>}

2 Responses to “Bug in Flex SDK?”

  1. Philippe says:

    The problem is both your LoadStuff project and SideMenu have a main “Main” class.
    Use a package (or a different main class name) to keep them different.

  2. Thomas says:

    Ah, I didn’t see that conflict. Thanks a lot! :-)

Leave a Reply