<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cata.printBlog(); &#187; actionscript2</title>
	<atom:link href="http://cata.onsysol.com/blog/tag/actionscript2/feed/" rel="self" type="application/rss+xml" />
	<link>http://cata.onsysol.com/blog</link>
	<description>... it&#039;s about software!</description>
	<lastBuildDate>Sun, 30 May 2010 13:19:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AS2Loader &#8211; Loading AS2 into AS3</title>
		<link>http://cata.onsysol.com/blog/2009/10/as2loader-loading-as2-into-as3/</link>
		<comments>http://cata.onsysol.com/blog/2009/10/as2loader-loading-as2-into-as3/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 19:09:54 +0000</pubDate>
		<dc:creator>Catalin Ciocov</dc:creator>
				<category><![CDATA[Action Script]]></category>
		<category><![CDATA[actionscript2]]></category>
		<category><![CDATA[actionscript3]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://cata.onsysol.com/blog/?p=11</guid>
		<description><![CDATA[Loading as AS2 SWF into AS3 is possible using the Loader class, but that won&#8217;t help in case you also need to communicate with the loaded AS2 SWF. The trick is to create a wrapper AS2 SWF that will act as a proxy (or say bridge) between the host AS3 SWF and the target AS2 [...]]]></description>
			<content:encoded><![CDATA[<p>Loading as AS2 SWF into AS3 is possible using the Loader class, but that won&#8217;t help in case you also need to communicate with the loaded AS2 SWF. The trick is to create a wrapper AS2 SWF that will act as a proxy (or say bridge) between the host AS3 SWF and the target AS2 SWF. The bridge SWF will be loaded in the host AS3 SWF while the target AS2 SWF will be loaded inside the bridge.</p>
<p>Because I needed this quite a lot in one of the applications I&#8217;m working on, I&#8217;ve decided to create a class (AS2Loader) that will simplify the process of loading and communicating with ActionScript 2 SWFs from ActionScript 3. Here&#8217;s what you can do with it:</p>
<p>Suppose you have an AS3 SWF in which you want to load (and then communicate with) an AS2 SWF named &#8220;as2movieclip.swf&#8221;. Using the AS2Loader class you will do something like this:</p>
<pre>import com.onsysol.as3.AS2Loader.*;

var as2ldr:AS2Loader = new AS2Loader();
as2ldr.addEventListener('AS2LoadInit', onAS2LoadInit);
as2ldr.load(new URLRequest('as2movieclip.swf'));

// this is triggered when the 1st frame of "as2movieclip.swf"
// is loaded and initialized:
function onAS2LoadInit(e:AS2LoaderEvent) {
    addChild(AS2Loader(e.target));
}</pre>
<p>The above code will load the AS2 SWF and once loaded it will add it to the display list. Next, you can register event listeners or make function calls on the AS2 SWF. The nice thing about this is that you can also request to receive back the return value of the function calls you make (since under the hood this is using Local Connection objects the return value of the function calls you make will be delivered through AS2LoaderEvents). So let&#8217;s proceed.</p>
<p>Suppose our AS2 SWF dispatches an &#8220;ABC&#8221; event. The following piece of code will register an event listener for this event:</p>
<pre>as2ldr.addEventListenerOnAS2('ABC', onABCEvent);
...
function onABCEvent(e:AS2LoaderEvent) {
    // the original AS2 event object is accessible through
    // e.data.eventObj:
    trace(e.data.eventObj.type + ' event dispatched from AS2.');
}</pre>
<p>If you want to call a function on the AS2 SWF (eg: gotoAndPlay() or stop() or anything else that&#8217;s public) you&#8217;ll do it like this (the following code assumes there&#8217;s a function say(s:String) defined on the AS2 SWF):</p>
<pre>as2ldr.callAS2Method('say', 'hello from AS3');</pre>
<p>In case you need to call a function on the AS2 SWF and also get the return value there&#8217;s no problem. Assuming there&#8217;s a function sum(a, b) that returns the sum of a and b, the following code will call sum() on the AS2 and also request and process the return value.</p>
<pre>as2ldr.getAS2MethodResult('sum', 2, 5);

// to get the return value from sum, we need to register an event
// listener on the AS2Loader object:
as2ldr.addEventListener('onAS2sumResult', onAS2SumResult);

function onAS2SumResult(e:AS2LoaderEvent) {
    trace('The result of 2 + 5 is ' + e.data.result);
}</pre>
<p>As you can see from the above example, if you call a function and also request its return value, the AS2Loader object will dispatch an event named &#8220;onAS2[function name]Result&#8221; when the return value is available.</p>
<p>I&#8217;ve created a test/demo page <a href="http://cata.onsysol.com/as2loader/" target="_blank">here</a>. You can also <a href="http://cata.onsysol.com/downloads/as2loader.zip">download the AS2Loader class and the example page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cata.onsysol.com/blog/2009/10/as2loader-loading-as2-into-as3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
