<?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>残酷の青春 &#187; as3</title>
	<atom:link href="http://www.cruelyouth.com/tags/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cruelyouth.com</link>
	<description>journey continues</description>
	<lastBuildDate>Thu, 29 Jul 2010 22:33:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>运行时共享库（runtime share lib）</title>
		<link>http://www.cruelyouth.com/2008/05/16/%e8%bf%90%e8%a1%8c%e6%97%b6%e5%85%b1%e4%ba%ab%e5%ba%93%ef%bc%88runtime-share-lib%ef%bc%89/</link>
		<comments>http://www.cruelyouth.com/2008/05/16/%e8%bf%90%e8%a1%8c%e6%97%b6%e5%85%b1%e4%ba%ab%e5%ba%93%ef%bc%88runtime-share-lib%ef%bc%89/#comments</comments>
		<pubDate>Fri, 16 May 2008 02:37:57 +0000</pubDate>
		<dc:creator>Mars</dc:creator>
				<category><![CDATA[FLEX & ACTION SCRIPT]]></category>
		<category><![CDATA[as3]]></category>

		<guid isPermaLink="false">http://new.cruelyouth.com/?p=17</guid>
		<description><![CDATA[运行时共享库的作用是在swf中动态加载外部库，使用库中的资源等等，以下是例子：
1. 新建一fla，命名为Lib.fla
2. 在fla中新建一个元件，库面板中右键选择“链接”，类名设置为MyCircle
3. 新建一as文件，命名为MyCircle.as(空类)，代码如下

1
2
3
4
5
package&#123;
	import flash.display.*;
	public class MyCircle extends Sprite&#123;
	&#125;
&#125;

4. Lib.fla的文档类处写LibTest，然后新建一LibTest.as，代码如下

1
2
3
4
5
6
7
8
9
package&#123;
	import flash.display.*;
	public class LibTest extends Sprite&#123;
		public var CircleClass:Class = MyCircle;
		public function echo&#40;string:String&#41;&#123;
			trace&#40;&#34;echo from lib:&#34; + string&#41;;
		&#125;
	&#125;
&#125;

5. shift + f12 导出Lib.swf
6. 新建一app.fla，文档类设置为AppTest
7. 新建AppTest.as，代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package&#123;
	import flash.display.*;
	import flash.net.*;
	import flash.events.*;
&#160;
	public class AppTest extends Sprite&#123;
		public function AppTest &#40;&#41;&#123;
			init&#40;&#41;;
		&#125;
&#160;
		public function init&#40;&#41;:void&#123;
			var loader:Loader = new Loader&#40;&#41;;
			var req:URLRequest = new URLRequest&#40;&#41;;
			req.url = &#34;Lib.swf&#34;;
			loader.contentLoaderInfo.addEventListener&#40;Event.COMPLETE, completeHandler&#41;;
			loader.load&#40;req&#41;;
		&#125;
&#160;
		public function [...]]]></description>
			<content:encoded><![CDATA[<p>运行时共享库的作用是在swf中动态加载外部库，使用库中的资源等等，以下是例子：</p>
<p>1. 新建一fla，命名为Lib.fla<br />
2. 在fla中新建一个元件，库面板中右键选择“链接”，类名设置为MyCircle<br />
3. 新建一as文件，命名为MyCircle.as(空类)，代码如下</p>

<div class="wp_codebox"><table width="100%" ><tr id="174"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="17code4"><pre class="actionscript">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyCircle <span style="color: #0066CC;">extends</span> Sprite<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>4. Lib.fla的文档类处写LibTest，然后新建一LibTest.as，代码如下</p>

<div class="wp_codebox"><table width="100%" ><tr id="175"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="17code5"><pre class="actionscript">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">*</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LibTest <span style="color: #0066CC;">extends</span> Sprite<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> CircleClass:<span style="color: #000000; font-weight: bold;">Class</span> = MyCircle;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> echo<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">string</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;echo from lib:&quot;</span> + <span style="color: #0066CC;">string</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>5. shift + f12 导出Lib.swf<br />
6. 新建一app.fla，文档类设置为AppTest<br />
7. 新建AppTest.as，代码如下</p>

<div class="wp_codebox"><table width="100%" ><tr id="176"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="17code6"><pre class="actionscript">package<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">*</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AppTest <span style="color: #0066CC;">extends</span> Sprite<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> AppTest <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> loader:Loader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> req:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			req.<span style="color: #0066CC;">url</span> = <span style="color: #ff0000;">&quot;Lib.swf&quot;</span>;
			loader.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, completeHandler<span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> completeHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> loader:Loader = <span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">target</span> as LoaderInfo<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">loader</span>;
			<span style="color: #000000; font-weight: bold;">var</span> lib:LibTest = LibTest<span style="color: #66cc66;">&#40;</span>loader.<span style="color: #006600;">content</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>lib<span style="color: #66cc66;">&#41;</span>;
			lib.<span style="color: #006600;">echo</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;xxx&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> lib.<span style="color: #006600;">CircleClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>8. 导出运行一下，可以看到Lib.swf被加载以后，AppTest调用lib中的MyCircle类新建了一个圆形加载了舞台上。</p>
<p>这种方法可以用来动态加载字体，或其他比较大但不是必须的资源。<br />
注意这种方法和flash 本身所的runtime sharelib应该是不一样的，flash的这个概念是可以将原件按share lib导出，然后再fla中可以导入swf得到swf中的库元件，这个运行时是指flash开发工具的运行时而不是swf的运行时。</p>
<p>下载本文源代码：<a href='http://new.cruelyouth.com/wp-content/uploads/2008/05/sharelib.rar'>sharelib</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cruelyouth.com/2008/05/16/%e8%bf%90%e8%a1%8c%e6%97%b6%e5%85%b1%e4%ba%ab%e5%ba%93%ef%bc%88runtime-share-lib%ef%bc%89/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
