<?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>Lam&#039;s Blog &#187; Development &amp; Design</title>
	<atom:link href="http://junnan.org/blog/?feed=rss2&#038;cat=4" rel="self" type="application/rss+xml" />
	<link>http://junnan.org/blog</link>
	<description></description>
	<lastBuildDate>Thu, 05 Aug 2010 03:34:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>[AS3]利用ByteArray压缩游戏关卡数据</title>
		<link>http://junnan.org/blog/?p=1147</link>
		<comments>http://junnan.org/blog/?p=1147#comments</comments>
		<pubDate>Sun, 06 Jun 2010 09:59:18 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1147</guid>
		<description><![CDATA[ByteArray 类提供用于优化读取、写入以及处理二进制数据的方法和属性。其中可以利用compress方法来压缩程序中过长的字符串，非常有效，特别是用在可以设计关卡生成代码的小游戏中，这里要... ]]></description>
			<content:encoded><![CDATA[<p>ByteArray 类提供用于优化读取、写入以及处理二进制数据的方法和属性。其中可以利用compress方法来压缩程序中过长的字符串，非常有效，特别是用在可以设计关卡生成代码的小游戏中，这里要用到subhero写的一个<a href='http://junnan.org/blog/wp-content/uploads/2010/06/Base64.zip'>Base64类</a>，方法如下：<br />
<span id="more-1147"></span><br />
压缩：</p>

<div class="wp_codebox"><table><tr id="p11473"><td class="code" id="p1147code3"><pre class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> Compress<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> textBytes:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	textBytes.<span style="color: #006600;">writeUTFBytes</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>;
	textBytes.<span style="color: #006600;">compress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> Base64.<span style="color: #006600;">Encode</span><span style="color: #66cc66;">&#40;</span>textBytes<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>解压缩：</p>

<div class="wp_codebox"><table><tr id="p11474"><td class="code" id="p1147code4"><pre class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> UnCompress<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> textBytes:ByteArray = Base64.<span style="color: #006600;">Decode</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">try</span>
		textBytes.<span style="color: #006600;">uncompress</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:IOError<span style="color: #66cc66;">&#41;</span> 
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The code to uncompress is not valid.&quot;</span><span style="color: #66cc66;">&#41;</span>;	
	<span style="color: #b1b100;">return</span> textBytes.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=1143" title="10个不错的Flash开发资源网站">10个不错的Flash开发资源网站 (1)</a></li>
<li><a href="http://junnan.org/blog/?p=1117" title="Flash在线独立游戏开发框架:Flixel">Flash在线独立游戏开发框架:Flixel (1)</a></li>
<li><a href="http://junnan.org/blog/?p=1077" title="基于Box2DFlash的碰撞检测管理">基于Box2DFlash的碰撞检测管理 (0)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1147</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10个不错的Flash开发资源网站</title>
		<link>http://junnan.org/blog/?p=1143</link>
		<comments>http://junnan.org/blog/?p=1143#comments</comments>
		<pubDate>Wed, 12 May 2010 06:48:48 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1143</guid>
		<description><![CDATA[ActionScript Library on senocular.com Actionscript Library on actionscript.org Open Source Flash Projects on osflash.org Spark project FFILES v3.0 Jim’s Flash Bestiary gotoandlearn.com Flash and ActionScript Tutorials on kirupa.com flashkit.com Tile ba... ]]></description>
			<content:encoded><![CDATA[<ul>
<li><a href="http://www.senocular.com/flash/actionscript.php" target="_blank">ActionScript Library on senocular.com</a><br />
<span id="more-1143"></span></p>
<p><a href="http://www.senocular.com/flash/actionscript.php" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/ActionScriptLibraryonsenocular_thumb.png" title="ActionScriptLibraryonsenocular" class="alignnone" width="450" height="216" /></a></li>
<li><a href="http://www.actionscript.org/actionscripts_library/" target="_blank">Actionscript Library on actionscript.org</a>
<p><a href="http://www.actionscript.org/actionscripts_library/" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/ActionscriptLibraryonactionscript_thumb.png" title="ActionscriptLibraryonactionscript" class="alignnone" width="443" height="504" /></a></li>
<li><a href="http://osflash.org/projects" target="_blank">Open Source Flash Projects on osflash.org</a>
<p><a href="http://osflash.org/projects" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/OpenSourceFlashProjectsonosflash_thumb.png" title="OpenSourceFlashProjectsonosflash" class="alignnone" width="450" height="326" /></a></li>
<li><a href="http://www.libspark.org/wiki/WikiStart/en" target="_blank">Spark project</a>
<p><a href="http://www.libspark.org/wiki/WikiStart/en" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/Sparkproject_thumb.png" title="Sparkproject" class="alignnone" width="450" height="257" /></a></li>
<li><a href="http://www.ffiles.com/" target="_blank">FFILES v3.0</a><br /><a href="http://www.ffiles.com/" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/FFILESv3_thumb.png" title="FFILESv3" class="alignnone" width="450" height="343" /></a></li>
<li><a href="http://www.krazydad.com/bestiary/" target="_blank">Jim’s Flash Bestiary</a><br /><a href="http://www.krazydad.com/bestiary/" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/JimsFlashBestiary_thumb.png" title="JimsFlashBestiary" class="alignnone" width="450" height="317" /></a></li>
<li><a href="http://www.gotoandlearn.com/" target="_blank">gotoandlearn.com</a>
<p><a href="http://www.gotoandlearn.com/" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/gotoandlearn_thumb.png" title="gotoandlearn" class="alignnone" width="450" height="302" /></a></li>
<li><a href="http://www.kirupa.com/developer/flash/index.htm" target="_blank">Flash and ActionScript Tutorials on kirupa.com</a>
<p><a href="http://www.kirupa.com/developer/flash/index.htm" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/FlashandActionScriptTutorialsonkirupa_thumb.png" title="FlashandActionScriptTutorialsonkirupa" class="alignnone" width="450" height="330" /></a></li>
<li><a href="http://www.flashkit.com/" target="_blank">flashkit.com</a>
<p><a href="http://www.flashkit.com/" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/flashkit_thumb.png" title="flashkit" class="alignnone" width="450" height="336" /></a></li>
<li><a href="http://www.tonypa.pri.ee/tbw/start.html" target="_blank">Tile based games ( For Flash Game Developers )</a>
<p><a href="http://www.tonypa.pri.ee/tbw/start.html" target="_blank"><img alt="" src="http://ntt.cc/wp-content/uploads/2010/05/Tilebasedgames_thumb.png" title="Tilebasedgames" class="alignnone" width="450" height="332" /></a></li>
</ul>
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
<li><a href="http://junnan.org/blog/?p=1117" title="Flash在线独立游戏开发框架:Flixel">Flash在线独立游戏开发框架:Flixel (1)</a></li>
<li><a href="http://junnan.org/blog/?p=1077" title="基于Box2DFlash的碰撞检测管理">基于Box2DFlash的碰撞检测管理 (0)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1143</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>对FlashBuilder或FDT开发者可能有用的几个插件</title>
		<link>http://junnan.org/blog/?p=1139</link>
		<comments>http://junnan.org/blog/?p=1139#comments</comments>
		<pubDate>Sat, 08 May 2010 14:35:41 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[Share]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[FDT]]></category>
		<category><![CDATA[Flash Builder]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1139</guid>
		<description><![CDATA[ExploreFS &#8211; http://www.junginger.biz/eclipse/ 这是一个可以在IDE的文件管理器中方便打开文件所在文件夹的工具，之前一用过easyexplorer，但发现这个插件更新缓慢，在最新的Flash Builer 4中已经无法使... ]]></description>
			<content:encoded><![CDATA[<p><strong>ExploreFS</strong> &#8211; http://www.junginger.biz/eclipse/<br />
这是一个可以在IDE的文件管理器中方便打开文件所在文件夹的工具，之前一用过easyexplorer，但发现这个插件更新缓慢，在最新的Flash Builer 4中已经无法使用了。</p>
<p><strong>BuildMarker</strong> &#8211; http://flashteam.tencent.com/downloads/BuildMarker/<br />
腾讯flash团队里的Element修改自snowkit.cnr的一个自动记录版本号的插件，比较实用，使用方法可以到<a href="http://flashteam.tencent.com/post/77/flexbuilder-3-%E8%87%AA%E5%8A%A8%E8%AE%B0%E5%BD%95-project-%E7%89%88%E6%9C%AC%E5%8F%B7-buildmarker-%E6%8F%92%E4%BB%B6/" target="_blank">这里</a>查看。</p>
<p><strong>Subclipse</strong> &#8211; http://subclipse.tigris.org/update_1.6.x<br />
这个就很熟悉了，SVN，就不多说了，其它插件可以不装我也必须要装上这个。</p>
<p><strong>FlexFormatter</strong> &#8211; http://flexformatter.googlecode.com/svn/trunk/FlexFormatter/FlexPrettyPrintCommandUpdateSite<br />
从插件名就可以看出，这是一款Flex代码格式化工具，当然也适合于纯AS代码项目，官方站点在<a href="http://sourceforge.net/projects/flexformatter/">这里</a>。<br />
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
<li><a href="http://junnan.org/blog/?p=1143" title="10个不错的Flash开发资源网站">10个不错的Flash开发资源网站 (1)</a></li>
<li><a href="http://junnan.org/blog/?p=1117" title="Flash在线独立游戏开发框架:Flixel">Flash在线独立游戏开发框架:Flixel (1)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1139</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flash在线独立游戏开发框架:Flixel</title>
		<link>http://junnan.org/blog/?p=1117</link>
		<comments>http://junnan.org/blog/?p=1117#comments</comments>
		<pubDate>Thu, 08 Apr 2010 09:16:43 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flixel]]></category>
		<category><![CDATA[游戏]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1117</guid>
		<description><![CDATA[是的，这是一个专门用于开发在线2D小游戏的框架，完全开源免费，能想到的常用功能大多都给集成了，动画、碰撞、物理加速、贴片地图一应俱全。 群里也有不少同学也讨论过Flixel，今天抽... ]]></description>
			<content:encoded><![CDATA[<p>是的，这是一个专门用于开发在线2D小游戏的框架，完全开源免费，能想到的常用功能大多都给集成了，动画、碰撞、物理加速、贴片地图一应俱全。<br />
群里也有不少同学也讨论过Flixel，今天抽空研究了一下，做了个小游戏Demo，感兴趣的同学不防试试。<br />
<a href="http://www.flixel.org">http://www.flixel.org</a><br />
<span id="more-1117"></span><br />
<embed src="http://junnan.org/blog/wp-content/uploads/2010/04/FlixelMiniGames.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="320" height="150"></embed></p>
<p>说明：回车键开始，方向键控制前进、后退以及跳跃。</p>
<p>源码下载：<a href='http://junnan.org/blog/wp-content/uploads/2010/04/FlixelMiniGames.zip'>FlixelMiniGames</a></p>
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=1152" title="旋转空间 &#8211; Rotate&#038;Roll">旋转空间 &#8211; Rotate&#038;Roll (9)</a></li>
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
<li><a href="http://junnan.org/blog/?p=1143" title="10个不错的Flash开发资源网站">10个不错的Flash开发资源网站 (1)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1117</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>基于Box2DFlash的碰撞检测管理</title>
		<link>http://junnan.org/blog/?p=1077</link>
		<comments>http://junnan.org/blog/?p=1077#comments</comments>
		<pubDate>Sun, 04 Apr 2010 10:05:03 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[box2d]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1077</guid>
		<description><![CDATA[去年的这个时候写过一篇关于Box2D的碰撞检测实现《Box2D中使用b2ContactListener进行碰撞检测》，虽然简单，但在需要检测大量碰撞的时候，所有需要在碰撞时触发的事件都要写到那一个继承自b2Con... ]]></description>
			<content:encoded><![CDATA[<p>去年的这个时候写过一篇关于Box2D的碰撞检测实现《<a href="http://junnan.org/blog/?p=490">Box2D中使用b2ContactListener进行碰撞检测</a>》，虽然简单，但在需要检测大量碰撞的时候，所有需要在碰撞时触发的事件都要写到那一个继承自b2ContactListener的类中未免显得杂乱，不易管理，如果能把刚体的碰撞处理分开写到各自的UserData类中看起来就好多了。</p>
<p>下面是实现方法和部分代码：<br />
Box2DFlash版本：2.1a<br />
首先需要为刚体的UserData写一个基类，所有用于刚体的UserData继承自该类，注册类中的contactHandle虚拟函数，最终用来在各自的UserData类中处理碰撞。</p>

<div class="wp_codebox"><table><tr id="p10779"><td class="code" id="p1077code9"><pre class="actionscript"><span style="color: #0066CC;">import</span> Box2D.<span style="color: #006600;">Dynamics</span>.<span style="color: #006600;">b2Fixture</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShapeBase <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> ShapeBase<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	 * 处理碰撞检测 
	 * @param shapeBase 与自己发生碰撞的刚体的UserData
	 * @param fixture 与自己发生碰撞的刚体的b2Fixture对象
	 * @param selfFixture 自己刚体的b2Fixture对象
	 */</span>
	<span style="color: #0066CC;">public</span> virtual <span style="color: #000000; font-weight: bold;">function</span> contactHandle<span style="color: #66cc66;">&#40;</span>shape:ShapeBase, fixture:b2Fixture, selfFixture:b2Fixture<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>然后是两个UserData类，当两个刚体发生碰撞时，就在各自类中的contactHandle方法(这里覆写了基类contactHandle方法）的里做碰撞后的处理。<br />
球形类:</p>

<div class="wp_codebox"><table><tr id="p107710"><td class="code" id="p1077code10"><pre class="actionscript"><span style="color: #0066CC;">import</span> Box2D.<span style="color: #006600;">Dynamics</span>.<span style="color: #006600;">b2Fixture</span>;
<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">lam</span>.<span style="color: #006600;">runtime</span>.<span style="color: #006600;">Global</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShapeBall <span style="color: #0066CC;">extends</span> ShapeBase
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ShapeBall<span style="color: #66cc66;">&#40;</span>x:<span style="color: #0066CC;">Number</span>, y:<span style="color: #0066CC;">Number</span>, radius:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// 处理碰撞的事件</span>
	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> contactHandle<span style="color: #66cc66;">&#40;</span>shape:ShapeBase, fixture:b2Fixture, selfFixture:b2Fixture<span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// 处理碰撞代码</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>矩形类：</p>

<div class="wp_codebox"><table><tr id="p107711"><td class="code" id="p1077code11"><pre class="actionscript"><span style="color: #0066CC;">import</span> Box2D.<span style="color: #006600;">Collision</span>.<span style="color: #006600;">Shapes</span>.<span style="color: #006600;">b2Shape</span>;
<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">lam</span>.<span style="color: #006600;">runtime</span>.<span style="color: #006600;">Global</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShapeRectangle <span style="color: #0066CC;">extends</span> ShapeBase
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ShapeRectangle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// 处理碰撞的事件</span>
	<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> contactHandle<span style="color: #66cc66;">&#40;</span>shape:ShapeBase, fixture:b2Fixture, selfFixture:b2Fixture<span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// 处理碰撞代码</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>然后写一个继承自b2ContactListener的碰撞检测类，在这里触发两个刚体UserData实例的碰撞处理方法。</p>

<div class="wp_codebox"><table><tr id="p107712"><td class="code" id="p1077code12"><pre class="actionscript"><span style="color: #0066CC;">import</span> Box2D.<span style="color: #006600;">Collision</span>.<span style="color: #66cc66;">*</span>;
<span style="color: #0066CC;">import</span> Box2D.<span style="color: #006600;">Dynamics</span>.<span style="color: #66cc66;">*</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
<span style="color: #0066CC;">import</span> gadgets.<span style="color: #006600;">ShapeBase</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContactListener <span style="color: #0066CC;">extends</span> b2ContactListener
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ContactListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><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> override <span style="color: #000000; font-weight: bold;">function</span> BeginContact<span style="color: #66cc66;">&#40;</span>contact:b2Contact<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// 得到发生碰撞的两个刚体的b2Fixture实例</span>
		<span style="color: #000000; font-weight: bold;">var</span> fixtureA:b2Fixture = contact.<span style="color: #006600;">GetFixtureA</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">var</span> fixtureB:b2Fixture = contact.<span style="color: #006600;">GetFixtureB</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">// 根据b2Fixture实例获得它们的UserData实例，当然这些实例都是继承自ShapeBase</span>
		<span style="color: #000000; font-weight: bold;">var</span> shapeA:ShapeBase = fixtureA.<span style="color: #006600;">GetUserData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ShapeBase;
		<span style="color: #000000; font-weight: bold;">var</span> shapeB:ShapeBase = fixtureB.<span style="color: #006600;">GetUserData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ShapeBase;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>fixtureA <span style="color: #66cc66;">&amp;&amp;</span> fixtureB<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// 执行两个碰撞刚体各自的碰撞处理</span>
			shapeA.<span style="color: #006600;">contactHandle</span><span style="color: #66cc66;">&#40;</span>shapeB, fixtureB, fixtureA<span style="color: #66cc66;">&#41;</span>;
			shapeB.<span style="color: #006600;">contactHandle</span><span style="color: #66cc66;">&#40;</span>shapeA, fixtureA, fixtureB<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>该文也发表于茶饭后博客-<a href="http://blog.chafanhou.com/?p=242">茶馆儿</a><br />
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=490" title="Box2D中使用b2ContactListener进行碰撞检测">Box2D中使用b2ContactListener进行碰撞检测 (2)</a></li>
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
<li><a href="http://junnan.org/blog/?p=1143" title="10个不错的Flash开发资源网站">10个不错的Flash开发资源网站 (1)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1077</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用XMLSocket获得SmartFoxServer的zone在线人数</title>
		<link>http://junnan.org/blog/?p=1033</link>
		<comments>http://junnan.org/blog/?p=1033#comments</comments>
		<pubDate>Sat, 19 Dec 2009 12:56:47 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[SmartFoxServer]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1033</guid>
		<description><![CDATA[在利用SmartFoxServer开发多人在线应用或游戏的时候，往往需要获取到指定zone的在线人数，官方自SFS 1.50版之后就给出了解决方法，我们可以利用服务器脚本语言或者actionscript来通过发送xml格式的... ]]></description>
			<content:encoded><![CDATA[<p>在利用SmartFoxServer开发多人在线应用或游戏的时候，往往需要获取到指定zone的在线人数，官方自SFS 1.50版之后就给出了解决方法，我们可以利用服务器脚本语言或者actionscript来通过发送xml格式的请求来获到，挺简单，写篇日志记一下actionscript利用xmlsocket来获取的例子：（如想用服务器脚本，请参照官方文档，见文末）</p>
<p>在写代码之前，首先要更改一下sfs的配置文件(config.xml)，将配置文件<strong>&lt;EnableZoneInfo&gt;true&lt;/EnableZoneInfo&gt;</strong>中的false改为true来允许获取zone的统计信息，这一步在项目发布后往服务器架设时常常被忘掉，导致在线人数数据无法返回，本人就有这样的惨痛教训。T_T<br />
<span id="more-1033"></span><br />
AS3代码如下：</p>

<div class="wp_codebox"><table><tr id="p103315"><td class="code" id="p1033code15"><pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">DataEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">XMLSocket</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ZoneCount
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _request:<span style="color: #0066CC;">String</span>;		
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _conn:<span style="color: #0066CC;">XMLSocket</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XMLSocket</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> count:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ZoneCount<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			_conn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #0066CC;">CONNECT</span>, <span style="color: #0066CC;">onConnect</span><span style="color: #66cc66;">&#41;</span>;
			_conn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #0066CC;">CLOSE</span>, <span style="color: #0066CC;">onClose</span><span style="color: #66cc66;">&#41;</span>;
			_conn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>DataEvent.<span style="color: #0066CC;">DATA</span>, <span style="color: #0066CC;">onData</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> conn<span style="color: #66cc66;">&#40;</span>ip:<span style="color: #0066CC;">String</span>, port:<span style="color: #0066CC;">int</span>, zoneName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// 向服务器发送的xml格式请求</span>
			_request = <span style="color: #ff0000;">&quot;&lt;msg t=<span style="color: #000099; font-weight: bold;">\&quot;</span>sys<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&lt;body r=<span style="color: #000099; font-weight: bold;">\&quot;</span>-1<span style="color: #000099; font-weight: bold;">\&quot;</span> action=<span style="color: #000099; font-weight: bold;">\&quot;</span>zInfo<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span>+zoneName+<span style="color: #ff0000;">&quot;&lt;/body&gt;&lt;/msg&gt;&quot;</span>;
			_conn.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span>ip, port<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">onConnect</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// 如果连接上，就发送请求</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_conn.<span style="color: #006600;">connected</span><span style="color: #66cc66;">&#41;</span>
				_conn.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span>_request<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">onClose</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;XMLSocket connect is close.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">onData</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DataEvent<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> <span style="color: #0066CC;">data</span>:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">data</span>;
			<span style="color: #808080; font-style: italic;">// 检查返回的数据中是否包含zInfo关键字</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;zInfo&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">xml</span>:<span style="color: #0066CC;">XML</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #808080; font-style: italic;">// 得到在线人数</span>
				count = <span style="color: #0066CC;">parseInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">xml</span>.<span style="color: #006600;">children</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
				_conn.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>另外在连接的时候如果遇到安全沙箱问题，解决方法有下面几种：<br />
一、在被请求的服务器开设843端口，提供策略内容，详见<a href="http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html" target="_blank">这里</a>。<br />
二、利用flash.system.Security的loadPolicyFile方法指定策略文件地址来获到策略内容。<br />
三、在swf根目录放置策略文件(crossdomain.xml)。（推荐）</p>
<p>下面是本实例要用到的策略文件内容：</p>

<div class="wp_codebox"><table><tr id="p103316"><td class="code" id="p1033code16"><pre class="xml"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;">&lt; !DOCTYPE cross-domain-policy SYSTEM <span style="color: #ff0000;">&quot;/xml/dtds/cross-domain-policy.dtd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- Policy file for xmlsocket://socks.example.com --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cross</span> -domain-policy<span style="color: #000000; font-weight: bold;">&gt;</span></span> 
&nbsp;
   <span style="color: #808080; font-style: italic;">&lt;!-- This is a master socket policy file --&gt;</span>
   <span style="color: #808080; font-style: italic;">&lt;!-- No other socket policies on the host will be permitted --&gt;</span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;site</span> -control <span style="color: #000066;">permitted-cross-domain-policies</span>=<span style="color: #ff0000;">&quot;all&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #808080; font-style: italic;">&lt;!-- Instead of setting to-ports=&quot;*&quot;, administrator's can use ranges and commas --&gt;</span>
   <span style="color: #808080; font-style: italic;">&lt;!-- This will allow access to ports 123, 456, 457 and 458 --&gt;</span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;allow</span> -access-from <span style="color: #000066;">domain</span>=<span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #000066;">to-ports</span>=<span style="color: #ff0000;">&quot;80,9339&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cross<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>参考自<a href="http://www.smartfoxserver.com/docs/docPages/tutorials_basic/17_zoneCount/index.htm" target="_blank">官方文档</a></p>
<p><strong>后记：</strong><br />
2009.12.12<br />
经测试，该方法返回的xml会有不完整的情况，暂时不知道是因为什么导致的。<br />
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=1027" title="用数据库读取和存储Smartfoxserver的好友列表(BuddyList)">用数据库读取和存储Smartfoxserver的好友列表(BuddyList) (15)</a></li>
<li><a href="http://junnan.org/blog/?p=994" title="使用java编写SmartFoxServer自定义安全验证登录扩展">使用java编写SmartFoxServer自定义安全验证登录扩展 (4)</a></li>
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1033</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>用数据库读取和存储Smartfoxserver的好友列表(BuddyList)</title>
		<link>http://junnan.org/blog/?p=1027</link>
		<comments>http://junnan.org/blog/?p=1027#comments</comments>
		<pubDate>Mon, 14 Dec 2009 00:46:46 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SmartFoxServer]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1027</guid>
		<description><![CDATA[SFS最新的Buddy List 2.0不再只支持文件存储好友列表，也可以支持数据库存储好友列表，提供更好的速度和安全性，每个Zone都可以配置自己的数据库。 我们可以通过一个Java类来负责好友列表的加... ]]></description>
			<content:encoded><![CDATA[<p>SFS最新的Buddy List 2.0不再只支持文件存储好友列表，也可以支持数据库存储好友列表，提供更好的速度和安全性，每个Zone都可以配置自己的数据库。<br />
我们可以通过一个Java类来负责好友列表的加载、保存，代码如下：</p>

<div class="wp_codebox"><table><tr id="p102718"><td class="code" id="p1027code18"><pre class="java"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.SmartFoxServer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.data.buddylist.StorableBuddyItem</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.data.buddylist.StorableBuddyList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.data.buddylist.persistence.AbstractBuddyPersister</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.data.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.db.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.extensions.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BuddyPersiter <span style="color: #000000; font-weight: bold;">extends</span> AbstractBuddyPersister 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> ExtensionHelper helper<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Zone currentZone<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> DbManager db<span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> param<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		helper <span style="color: #339933;">=</span> ExtensionHelper.<span style="color: #006633;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">currentZone</span> <span style="color: #339933;">=</span> helper.<span style="color: #006633;">getZone</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">zoneName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		info<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CUSTOM Persister initialized.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> param<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		info<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CUSTOM Persister shutting down.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> StorableBuddyList loadList<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> userName<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> loadBuddys<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 从数据库读取好友列表</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> StorableBuddyList loadBuddys<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> userName<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		db <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">currentZone</span>.<span style="color: #006633;">dbManager</span><span style="color: #339933;">;</span>
		StorableBuddyList buddyList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorableBuddyList<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		buddyList.<span style="color: #006633;">buddies</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>storablebuddyitem<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userName.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT BUDDYNAME,ISBLACK FROM BUDDYS WHERE USERNAME='&quot;</span><span style="color: #339933;">+</span>userName<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
			ArrayList<span style="color: #339933;">&lt;</span>datarow<span style="color: #339933;">&gt;</span> buddys <span style="color: #339933;">=</span> db.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>buddys.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				DataRow dr<span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> buddys.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					dr <span style="color: #339933;">=</span> buddys.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					StorableBuddyItem buddyItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorableBuddyItem<span style="color: #009900;">&#40;</span>dr.<span style="color: #006633;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BUDDYNAME&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					buddyItem.<span style="color: #006633;">isBlocked</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>dr.<span style="color: #006633;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ISBLACK&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
					buddyList.<span style="color: #006633;">buddies</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>buddyItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		info<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Buddy List loaded for user: &quot;</span> <span style="color: #339933;">+</span> userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> buddyList<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> saveList<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> userName, StorableBuddyList buddyList<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		db <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">currentZone</span>.<span style="color: #006633;">dbManager</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 删除所有好友</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM BUDDYS WHERE USERNAME='&quot;</span> <span style="color: #339933;">+</span> userName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
		db.<span style="color: #006633;">executeCommand</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		db.<span style="color: #006633;">executeCommand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;commit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> isBlack <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		info<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving buddy list for user: &quot;</span> <span style="color: #339933;">+</span> userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>StorableBuddyItem buddyItem <span style="color: #339933;">:</span> buddyList.<span style="color: #006633;">buddies</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&gt; &quot;</span> <span style="color: #339933;">+</span> buddyItem.<span style="color: #006633;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			isBlack <span style="color: #339933;">=</span> buddyItem.<span style="color: #006633;">isBlocked</span> <span style="color: #339933;">?</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			db.<span style="color: #006633;">executeCommand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO BUDDYS(ISBLACK, USERNAME, BUDDYNAME) VALUES(&quot;</span><span style="color: #339933;">+</span>isBlack<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,'&quot;</span><span style="color: #339933;">+</span>userName<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;','&quot;</span><span style="color: #339933;">+</span>buddyItem.<span style="color: #006633;">name</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			db.<span style="color: #006633;">executeCommand</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;commit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> Map<span style="color: #339933;">&lt;</span>string , String<span style="color: #339933;">&gt;</span> getOfflineVariables<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> userName<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		info<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getOfflineVariables called for user: &quot;</span> <span style="color: #339933;">+</span> userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;&lt;</span>string , String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> removeOfflineBuddy<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> owner, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> buddyName<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Called when removing a buddy that is currently offline</span>
		<span style="color: #666666; font-style: italic;">// and the mutualRemove is active </span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> info<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> msg<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		SmartFoxServer.<span style="color: #006633;">log</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;&lt;/</span>datarow<span style="color: #339933;">&gt;&lt;/</span>storablebuddyitem<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>写完后，将BuddyPersiter.class文件复制到SFS的Server目录中（不是javaExtensions)，然后在SFS配置文件的相应zone中加入如下buddylist配置(注意persisterClass标签内的类名)：<br />
<span id="more-1027"></span></p>
<pre>
&lt;BuddyList active=&quot;true&quot;&gt;
    &lt;size&gt;100&lt;/size&gt;
    &lt;maxBuddyVariables&gt;5&lt;/maxBuddyVariables&gt;
    &lt;mode&gt;advanced&lt;/mode&gt;

    &lt;!-- Apply only for advanced mode --&gt;
    &lt;addBuddyPermission&gt;true&lt;/addBuddyPermission&gt;
    &lt;offLineBuddyVariables&gt;true&lt;/offLineBuddyVariables&gt;
    &lt;permissionTimeOut&gt;10&lt;/permissionTimeOut&gt;
    &lt;mutualAddBuddy&gt;true&lt;/mutualAddBuddy&gt;
    &lt;mutualRemoveBuddy&gt;true&lt;/mutualRemoveBuddy&gt;
    &lt;persisterClass&gt;BuddyPersiter&lt;/persisterClass&gt;
&lt;/BuddyList&gt;
</pre>
<p>最后重启一下SFS，搞定。</p>
<p>参考自<a href="http://www.smartfoxserver.com/docs/docPages/sfsPro/buddyList2.htm" target="_blank">官方文档</a><br />
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=994" title="使用java编写SmartFoxServer自定义安全验证登录扩展">使用java编写SmartFoxServer自定义安全验证登录扩展 (4)</a></li>
<li><a href="http://junnan.org/blog/?p=1033" title="用XMLSocket获得SmartFoxServer的zone在线人数">用XMLSocket获得SmartFoxServer的zone在线人数 (1)</a></li>
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1027</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>AS3数组分页类</title>
		<link>http://junnan.org/blog/?p=1017</link>
		<comments>http://junnan.org/blog/?p=1017#comments</comments>
		<pubDate>Tue, 20 Oct 2009 14:11:24 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[as3]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1017</guid>
		<description><![CDATA[整整一个月没更新博客了，时间过的好快。。前几天本懒人为了实现一个分页功能，想在网上找个现成的AS3的分页类，找了半天没找到，只好自己写了个分页类，正好发上来。 这个类只有简单... ]]></description>
			<content:encoded><![CDATA[<p>整整一个月没更新博客了，时间过的好快。。前几天本懒人为了实现一个分页功能，想在网上找个现成的AS3的分页类，找了半天没找到，只好自己写了个分页类，正好发上来。<br />
这个类只有简单的向前翻页和向后翻页，以及显示当前页码和总页数的功能（类似于QQ农场的好友列表下面的翻页），翻到最前页或最后页继续翻，会循环到最后页或最前页，也算是看了QQ农场这个功能之后在用户体验方面的小改进，在类里面读到UI资源用到的ResourcesManager将在下篇日志中发出来。<br />
简陋的截图：<br />
<a href="http://junnan.org/blog/wp-content/uploads/2009/10/pager.png"><img src="http://junnan.org/blog/wp-content/uploads/2009/10/pager.png" alt="pager" title="pager" width="277" height="324" class="alignnone size-full wp-image-1018" /></a></p>

<div class="wp_codebox"><table><tr id="p101721"><td class="code" id="p1017code21"><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: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> common.<span style="color: #006600;">ResourcesManager</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// 数组分页类</span>
	<span style="color: #808080; font-style: italic;">// by Bindiry</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pager
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// 每个页面显示记录数</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _pageSize:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">// 总记录数</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _recordCount:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">// 当前页面</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _currentPage:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span>;
		<span style="color: #808080; font-style: italic;">// 页面总数</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _pageCount:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">// 页码信息</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _pageString:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #808080; font-style: italic;">// 页面信息及控制容器</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _pagePanel:Sprite;
		<span style="color: #808080; font-style: italic;">// 开始记录数</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _startRecord:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">// 结束记录数</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _endRecord:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
		<span style="color: #808080; font-style: italic;">// 页面改变事件相关</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _eventDispatcher:EventDispatcher;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const PAGE_CHANGED:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;changed&quot;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Pager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// 读取页码资源(箭头)</span>
			<span style="color: #000000; font-weight: bold;">var</span> clsPageArrow:<span style="color: #000000; font-weight: bold;">Class</span> = ResourcesManager.<span style="color: #006600;">getResource</span><span style="color: #66cc66;">&#40;</span>ResourcesManager.<span style="color: #006600;">UI</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;PagerArrow&quot;</span><span style="color: #66cc66;">&#93;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;
			<span style="color: #000000; font-weight: bold;">var</span> pageArrowLeft:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> clsPageArrow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			pageArrowLeft.<span style="color: #006600;">buttonMode</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #808080; font-style: italic;">// 设置向前翻页单击事件</span>
			pageArrowLeft.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, onLeftClickHandler<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> pageArrowRight:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> clsPageArrow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			pageArrowRight.<span style="color: #006600;">buttonMode</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #808080; font-style: italic;">// 设置向后翻页单击事件</span>
			pageArrowRight.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, onRightClickHandler<span style="color: #66cc66;">&#41;</span>;
			pageArrowRight.<span style="color: #006600;">scaleX</span> = -<span style="color: #cc66cc;">1</span>;
			<span style="color: #808080; font-style: italic;">// 将按钮及页码信息加入容器里</span>
			_pagePanel = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			_pagePanel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>pageArrowLeft<span style="color: #66cc66;">&#41;</span>;
			_pageString = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			_pageString.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>;
			_pagePanel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>_pageString<span style="color: #66cc66;">&#41;</span>;
			pageArrowRight.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">60</span>;
			_pagePanel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>pageArrowRight<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// 自定义事件</span>
			_eventDispatcher = <span style="color: #000000; font-weight: bold;">new</span> EventDispatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 向前翻页单击事件处理</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onLeftClickHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_currentPage <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				_currentPage -= <span style="color: #cc66cc;">1</span>;
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_currentPage == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
					_startRecord = <span style="color: #cc66cc;">1</span>;
				<span style="color: #b1b100;">else</span>
					_startRecord = <span style="color: #66cc66;">&#40;</span>_currentPage - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> _pageSize + <span style="color: #cc66cc;">1</span>;
				_endRecord = _startRecord + _pageSize;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				_currentPage = _pageCount;
				_startRecord = <span style="color: #66cc66;">&#40;</span>_currentPage - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> _pageSize + <span style="color: #cc66cc;">1</span>;
				<span style="color: #808080; font-style: italic;">// 取余</span>
				<span style="color: #000000; font-weight: bold;">var</span> _residueRecord:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span>_recordCount <span style="color: #66cc66;">%</span> _pageSize == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ? _pageSize : _recordCount <span style="color: #66cc66;">%</span> _pageSize;
				_endRecord = _startRecord + _residueRecord;
			<span style="color: #66cc66;">&#125;</span>
			updatePageString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> event:Event = <span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>PAGE_CHANGED<span style="color: #66cc66;">&#41;</span>;
			_eventDispatcher.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 向后翻页单击事件处理</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onRightClickHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_currentPage <span style="color: #66cc66;">&lt;</span> _pageCount<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				_currentPage += <span style="color: #cc66cc;">1</span>;
				_startRecord = <span style="color: #66cc66;">&#40;</span>_currentPage - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> _pageSize + <span style="color: #cc66cc;">1</span>;
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_currentPage == _pageCount<span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #808080; font-style: italic;">// 取余</span>
					<span style="color: #000000; font-weight: bold;">var</span> _residueRecord:<span style="color: #0066CC;">int</span> = <span style="color: #66cc66;">&#40;</span>_recordCount <span style="color: #66cc66;">%</span> _pageSize == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ? _pageSize : _recordCount <span style="color: #66cc66;">%</span> _pageSize;
					_endRecord = _startRecord + _residueRecord;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span>
				<span style="color: #66cc66;">&#123;</span>
					_endRecord = _startRecord + _pageSize;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				_currentPage = <span style="color: #cc66cc;">1</span>;
				_startRecord = <span style="color: #cc66cc;">1</span>;
				_endRecord = _startRecord + _pageSize;
			<span style="color: #66cc66;">&#125;</span>
			updatePageString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> event:Event = <span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>PAGE_CHANGED<span style="color: #66cc66;">&#41;</span>;
			_eventDispatcher.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 添加事件</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, fun:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			_eventDispatcher.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, fun<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 设置每页记录数</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> pageSize<span style="color: #66cc66;">&#40;</span>pagesize:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			_pageSize = pagesize;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 设置当前页码</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> currentPage<span style="color: #66cc66;">&#40;</span>currentPage:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			_currentPage = currentPage;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 设置记录总数</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> recordCount<span style="color: #66cc66;">&#40;</span>recordCount:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			_recordCount = recordCount;
			_pageCount = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">ceil</span><span style="color: #66cc66;">&#40;</span>_recordCount <span style="color: #66cc66;">/</span> _pageSize<span style="color: #66cc66;">&#41;</span>;
			updatePageString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 更新页码信息</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> updatePageString<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			_pageString.<span style="color: #0066CC;">text</span> = _currentPage + <span style="color: #ff0000;">&quot;/&quot;</span> + _pageCount;
		<span style="color: #66cc66;">&#125;</span>	
&nbsp;
		<span style="color: #808080; font-style: italic;">// 获取页码及控制按钮面板	</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> pagePanel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _pagePanel;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 获取起始记录数</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> startRecord<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _startRecord;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// 获取结束记录数</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> endRecord<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _endRecord;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>使用方法：（大概代码，有点麻烦，但大概思路是讲明白了，因为是从项目中提取的，所以本懒人未测试-_-）
</pre>

<div class="wp_codebox"><table><tr id="p101722"><td class="code" id="p1017code22"><pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> common.<span style="color: #006600;">Pager</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// 好友面板</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Test
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> arr:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FriendsPanel<span style="color: #66cc66;">&#40;</span>owner:DisplayObjectContainer<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
&nbsp;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">14</span>; i++<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
					arr.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>id:<span style="color: #cc66cc;">100</span>, <span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;bindiry&quot;</span>+i.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> pager:Pager = <span style="color: #000000; font-weight: bold;">new</span> Pager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			pager.<span style="color: #006600;">pageSize</span> = <span style="color: #cc66cc;">5</span>;
			pager.<span style="color: #006600;">recordCount</span> = arr.<span style="color: #0066CC;">length</span>;
			pager.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Pager.<span style="color: #006600;">PAGE_CHANGED</span>, onPagerChangedHandler<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// 将分页显示控制Panel添加到场景中</span>
			pager.<span style="color: #006600;">pagePanel</span>.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">260</span>;
			pager.<span style="color: #006600;">pagePanel</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pager.<span style="color: #006600;">pagePanel</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">// 页面更改事件中做相应处理</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onPagerChangedHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//清除场景</span>
			cleanStage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//往场景里加入数组项</span>
			<span style="color: #000000; font-weight: bold;">var</span> itemHeighti:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #000000; font-weight: bold;">var</span> itemHeight:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">200</span> <span style="color: #66cc66;">/</span> _PageSize;
			<span style="color: #000000; font-weight: bold;">var</span> currentHeight:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;startRecord&quot;</span>, startRecord<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;endRecord&quot;</span>, endRecord<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = startRecord - <span style="color: #cc66cc;">1</span>; i <span style="color: #66cc66;">&lt;</span> endRecord - <span style="color: #cc66cc;">1</span>; i++<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				currentHeight = <span style="color: #66cc66;">&#40;</span>itemHeighti <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ? itemHeight <span style="color: #66cc66;">*</span> itemHeighti : <span style="color: #cc66cc;">0</span>;
				addToListPanel<span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">name</span>, currentHeight, itemHeighti<span style="color: #66cc66;">&#41;</span>;
				itemHeighti += <span style="color: #cc66cc;">1</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addToListPanel<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">text</span>:<span style="color: #0066CC;">String</span>, <span style="color: #0066CC;">height</span>:<span style="color: #0066CC;">int</span>, i:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// 这里的_ListPanelItemBG是我在flash里做的一个MovieClip，然后在上面加了一个名称为itemText的动态文本</span>
			<span style="color: #000000; font-weight: bold;">var</span> _ListPanelItemBG:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> clsListItemBG<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			_ListPanelItemBG.<span style="color: #006600;">y</span> = <span style="color: #0066CC;">height</span>;
			_ListPanelItemBG.<span style="color: #006600;">itemText</span>.<span style="color: #0066CC;">text</span> = <span style="color: #0066CC;">text</span>;
			addChild<span style="color: #66cc66;">&#40;</span>_ListPanelItemBG<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> cleanStage<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: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">numChildren</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeChildAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</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>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>如果有朋友发现程序的不足或有更好的建议，还望在评论中提出。<br />
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
<li><a href="http://junnan.org/blog/?p=1143" title="10个不错的Flash开发资源网站">10个不错的Flash开发资源网站 (1)</a></li>
<li><a href="http://junnan.org/blog/?p=1139" title="对FlashBuilder或FDT开发者可能有用的几个插件">对FlashBuilder或FDT开发者可能有用的几个插件 (2)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1017</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BulkLoader加载swf(loader.content)为空问题解决</title>
		<link>http://junnan.org/blog/?p=1014</link>
		<comments>http://junnan.org/blog/?p=1014#comments</comments>
		<pubDate>Thu, 17 Sep 2009 17:02:23 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[BulkLoader]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=1014</guid>
		<description><![CDATA[平时想要加载一个swf文件里的实例对象时，只要用Loader去load它，然后用loader.content就会得到swf的实例对象直接使用，现在换成BulkLoader后，发现里面的LoadingItem对象并不能使用属性content获得实例... ]]></description>
			<content:encoded><![CDATA[<p>平时想要加载一个swf文件里的实例对象时，只要用Loader去load它，然后用loader.content就会得到swf的实例对象直接使用，现在换成BulkLoader后，发现里面的LoadingItem对象并不能使用属性content获得实例，debug发现在ImageItem.as中的onCompleteHandler事件处理函数里loader.content还是正常，只是无法正常传递给父类LoadingItem的_content属性。<br />
即然这样，还是按照<a href="http://junnan.org/blog/?p=989" title="用BulkLoader获取swf中的Class">获取Class</a>那篇日志的方法来解决，在LoadingItem.as中加入方法getContent用来覆写：<br />
<span id="more-1014"></span></p>

<div class="wp_codebox"><table><tr id="p101425"><td class="code" id="p1014code25"><pre class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getContent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>然后在ImageItem.as中加入属性swfContent，并在onCompleteHandler事件处理函数中把loader.content赋值给它，最后通过覆写getContent方法来把loader.content信息返回。</p>

<div class="wp_codebox"><table><tr id="p101426"><td class="code" id="p1014code26"><pre class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> swfContent:<span style="color: #0066CC;">Object</span>;
override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onCompleteHandler<span style="color: #66cc66;">&#40;</span>evt : Event<span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// TODO: test for the different behaviour when loading items with </span>
	<span style="color: #808080; font-style: italic;">// the a specific crossdomain and without one</span>
	<span style="color: #0066CC;">try</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// of no crossdomain has allowed this operation, this might</span>
		<span style="color: #808080; font-style: italic;">// raise a security error</span>
		_content = loader.<span style="color: #006600;">content</span>;
		swfContent = loader.<span style="color: #006600;">content</span>;
		<span style="color: #0066CC;">super</span>.<span style="color: #006600;">onCompleteHandler</span><span style="color: #66cc66;">&#40;</span>evt<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span><span style="color: #0066CC;">catch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span> : SecurityError<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// we can still use the Loader object (no dice for accessing it as data</span>
		<span style="color: #808080; font-style: italic;">// though. Oh boy:</span>
		_content = loader;
		<span style="color: #0066CC;">super</span>.<span style="color: #006600;">onCompleteHandler</span><span style="color: #66cc66;">&#40;</span>evt<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">// I am really unsure whether I should throw this event</span>
		<span style="color: #808080; font-style: italic;">// it would be nice, but simply delegating the error handling to user's code </span>
		<span style="color: #808080; font-style: italic;">// seems cleaner (and it also mimics the Standar API behaviour on this respect)</span>
		<span style="color: #808080; font-style: italic;">//onSecurityErrorHandler(e);</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>;</pre></td></tr></table></div>

<p>最后在使用时，直接调用LoadingItem对象的getContent方法即可。</p>
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=989" title="用BulkLoader获取swf中的Class">用BulkLoader获取swf中的Class (3)</a></li>
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
<li><a href="http://junnan.org/blog/?p=1143" title="10个不错的Flash开发资源网站">10个不错的Flash开发资源网站 (1)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=1014</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>使用java编写SmartFoxServer自定义安全验证登录扩展</title>
		<link>http://junnan.org/blog/?p=994</link>
		<comments>http://junnan.org/blog/?p=994#comments</comments>
		<pubDate>Sat, 12 Sep 2009 14:24:11 +0000</pubDate>
		<dc:creator>唯枫</dc:creator>
				<category><![CDATA[Development & Design]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SmartFoxServer]]></category>

		<guid isPermaLink="false">http://junnan.org/blog/?p=994</guid>
		<description><![CDATA[最近接触的东西有点杂，在写SmartFoxServer（以下简称SFS）服务端扩展时发现actionscript竟然只支持1.0，无奈只好用从来没有接触过的java来编写，参考官方cookbook及手册，成功实现了自定义安全验证... ]]></description>
			<content:encoded><![CDATA[<p>最近接触的东西有点杂，在写SmartFoxServer（以下简称SFS）服务端扩展时发现actionscript竟然只支持1.0，无奈只好用从来没有接触过的java来编写，参考官方cookbook及手册，成功实现了自定义安全验证登录扩展。<br />
系统用的是CentOS5.2(x86_64)，数据库是Oracle 11g，SFS版本为1.6.6。<br />
<span id="more-994"></span></p>
<ol>
<li>
<h3>配置</h3>
<p>数据库DEMO中的USERS表结构：</p>

<div class="wp_codebox"><table><tr id="p99432"><td class="code" id="p994code32"><pre class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">&quot;DEMO&quot;</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;USERS&quot;</span> 
<span style="color: #66cc66;">&#40;</span>	
<span style="color: #ff0000;">&quot;USERID&quot;</span> NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">38</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> ENABLE<span style="color: #66cc66;">,</span> 
<span style="color: #ff0000;">&quot;USERNAME&quot;</span> VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> ENABLE<span style="color: #66cc66;">,</span> 
<span style="color: #ff0000;">&quot;USERPWD&quot;</span> VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> ENABLE<span style="color: #66cc66;">,</span> 
<span style="color: #ff0000;">&quot;USEREMAIL&quot;</span> VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
 CONSTRAINT <span style="color: #ff0000;">&quot;TABLE1_PK&quot;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;USERID&quot;</span><span style="color: #66cc66;">&#41;</span> ENABLE
<span style="color: #66cc66;">&#41;</span> ;</pre></td></tr></table></div>

<p>将连接java连接oracle的驱动（oracle/product/11.1.0/db_1/jdbc/lib/ojdbc6.jar，这里我用的是JDK1.6，所以用ojdbc6.jar，根据自己的需要复制驱动）复制到（SFS_PRO_1.6.6/jre/lib/ext/）下，然后修改SFS根目录下的config.xml，在Zones节点内加入如下Zone配置：</p>

<div class="wp_codebox"><table><tr id="p99433"><td class="code" id="p994code33"><pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;zone</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Demo&quot;</span> <span style="color: #000066;">uCountUpdate</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">buddyList</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">maxUsers</span>=<span style="color: #ff0000;">&quot;4000&quot;</span> <span style="color: #000066;">customLogin</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rooms<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;room</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;The Hall&quot;</span> <span style="color: #000066;">maxUsers</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">isPrivate</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">isTemp</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">autoJoin</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">uCountUpdate</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rooms<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extension</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;demo_login&quot;</span>  <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;demo_login&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;databasemanager</span> <span style="color: #000066;">active</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;driver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>oracle.jdbc.driver.OracleDriver<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/driver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;connectionstring<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jdbc:oracle:thin:@localhost:1521:DEMO<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/connectionstring<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>				
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>demo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>demo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/password<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testsql<span style="color: #000000; font-weight: bold;">&gt;</span></span>&lt; !<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>SELECT COUNT<span style="color: #66cc66;">&#40;</span>*<span style="color: #66cc66;">&#41;</span> FROM USERS<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/testsql<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maxactive<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/maxactive<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maxidle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/maxidle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;onexhaustedpool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>fail<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/onexhaustedpool<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;blocktime<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5000<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/blocktime<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/databasemanager<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/zone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>这里根据自己数据库配置来设置驱动、连接字符串、用户名、密码，如果用的是mysql，那么驱动和连接字符串应该如下：</p>

<div class="wp_codebox"><table><tr id="p99434"><td class="code" id="p994code34"><pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;driver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.mysql.jdbc.Driver<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/driver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;connectionstring<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jdbc:mysql://localhost:3306/DEMO<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/connectionstring<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</li>
<li>
<h3>编写扩展</h3>
<p>在写扩展之前，需要了解一下安全验证的机制，借用一下官方的图示：<br />
<a href="http://junnan.org/blog/wp-content/uploads/2009/09/secureLoginDiagram.gif"><img src="http://junnan.org/blog/wp-content/uploads/2009/09/secureLoginDiagram.gif" alt="secureLoginDiagram" title="secureLoginDiagram" width="550" height="448" class="alignnone size-full wp-image-995" /></a><br />
<strong>第一步：</strong>从服务器获取随机码，把用户输入密码用md5加密，然后再把随机码和加密后的md5码再用md5加密，并发送给服务器。<br />
<strong>第二步：</strong>服务器端从数据库取出经过md5加密的用户密码，同样用md5再加密一次随机码和取出的md5密码，然后和客户端发来的hash过的字符进行对比，来验证登录。</p>
<p>理解了这次，就可以开始编写Zone配置里的扩展demo_login了，关于扩展的具体说明，看官方手册就可以了，这里给出源码借参考：</p>

<div class="wp_codebox"><table><tr id="p99435"><td class="code" id="p994code35"><pre class="java"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.nio.channels.SocketChannel</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.crypto.MD5</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.data.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.db.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.exceptions.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.extensions.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.lib.ActionscriptObject</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">it.gotoandplay.smartfoxserver.events.InternalEventObject</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> demo_login <span style="color: #000000; font-weight: bold;">extends</span> AbstractExtension
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> ExtensionHelper helper<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Zone currentZone<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> DbManager db<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		helper <span style="color: #339933;">=</span> ExtensionHelper.<span style="color: #006633;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">currentZone</span> <span style="color: #339933;">=</span> helper.<span style="color: #006633;">getZone</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getOwnerZone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		trace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Extension destroyed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> handleRequest<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> cmd, ActionscriptObject ao, User u, <span style="color: #000066; font-weight: bold;">int</span> fromRoom<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Your code here</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> handleRequest<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> cmd, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> params<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, User u, <span style="color: #000066; font-weight: bold;">int</span> fromRoom<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Your code here</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Handle Internal Server Events
	 * 
	 * @param ieo		the event object
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> handleInternalEvent<span style="color: #009900;">&#40;</span>InternalEventObject ieo<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ieo.<span style="color: #006633;">getEventName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;loginRequest&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// 根据配置文件里的DatabaseManager配置获得当前Zone的数据库操作对象</span>
			db <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">currentZone</span>.<span style="color: #006633;">dbManager</span><span style="color: #339933;">;</span>
			ActionscriptObject response <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActionscriptObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			User loginUser <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// 获取用户名</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> nick <span style="color: #339933;">=</span> ieo.<span style="color: #006633;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nick&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// 获取客户端利用服务器加密字符串和用户密码加密后MD5密码</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> ClientPassword <span style="color: #339933;">=</span> ieo.<span style="color: #006633;">getParam</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// 获取服务器socket通道</span>
			SocketChannel chan <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SocketChannel<span style="color: #009900;">&#41;</span>ieo.<span style="color: #006633;">getObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chan&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// 根据socket通道生成加密字符</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> ServerRandom <span style="color: #339933;">=</span> helper.<span style="color: #006633;">getSecretKey</span><span style="color: #009900;">&#40;</span>chan<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// 获取数据库中用户的密码</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aarraylist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ArrayList</span></a> arrList <span style="color: #339933;">=</span> db.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT USERPWD FROM USERS WHERE USERNAME='&quot;</span><span style="color: #339933;">+</span>nick<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> ServerPassword <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">boolean</span> IsLogin <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>arrList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_cmd&quot;</span>, <span style="color: #0000ff;">&quot;loginKO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;err&quot;</span>, <span style="color: #0000ff;">&quot;用户名不存在，登录失败。&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// 获取数据库密码</span>
				DataRow dr <span style="color: #339933;">=</span> arrList.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> dbpass <span style="color: #339933;">=</span> dr.<span style="color: #006633;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;USERPWD&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// 根据服务器加密字符和数据库用户密码，生成服务器端的混合MD5密码</span>
				ServerPassword <span style="color: #339933;">=</span> MD5.<span style="color: #006633;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getHash</span><span style="color: #009900;">&#40;</span>ServerRandom <span style="color: #339933;">+</span> dbpass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				IsLogin <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ClientPassword.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>ServerPassword<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>IsLogin<span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">try</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #666666; font-style: italic;">// 登录成功</span>
						loginUser <span style="color: #339933;">=</span> helper.<span style="color: #006633;">canLogin</span><span style="color: #009900;">&#40;</span>nick, ClientPassword, chan, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">currentZone</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_cmd&quot;</span>, <span style="color: #0000ff;">&quot;loginOK&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>loginUser.<span style="color: #006633;">getUserId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, loginUser.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>LoginException e<span style="color: #009900;">&#41;</span>
					<span style="color: #009900;">&#123;</span>
						<span style="color: #666666; font-style: italic;">// 登录失败</span>
						response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_cmd&quot;</span>, <span style="color: #0000ff;">&quot;loginKO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;err&quot;</span>, e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">else</span>
				<span style="color: #009900;">&#123;</span>
					response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_cmd&quot;</span>, <span style="color: #0000ff;">&quot;loginKO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					response.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;err&quot;</span>, <span style="color: #0000ff;">&quot;认证失败.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alinkedlist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">LinkedList</span></a> linkedlist <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alinkedlist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">LinkedList</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			linkedlist.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>chan<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// 返回结果给客户端</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">sendResponse</span><span style="color: #009900;">&#40;</span>response, <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>, <span style="color: #000066; font-weight: bold;">null</span>, linkedlist<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>IsLogin<span style="color: #009900;">&#41;</span>
				helper.<span style="color: #006633;">sendRoomList</span><span style="color: #009900;">&#40;</span>chan<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>这里有个关键点需要注意：配置文件中Zone标签的属性customLogin要设置成true才能使服务端响应loginRequest事件。
</pre>
</li>
<li>
<h3>客户端处理</h3>
<p>服务端的验证结果信息将通过客户端SmartFoxClient对象的onExtensionResponse返回，下面是该事件监听函数：</p>

<div class="wp_codebox"><table><tr id="p99436"><td class="code" id="p994code36"><pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onExtensionResponseHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:SFSEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// 数据传输类型</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">params</span>.<span style="color: #0066CC;">type</span>;
	<span style="color: #808080; font-style: italic;">// 用Object类型接收数据</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:<span style="color: #0066CC;">Object</span> = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">params</span>.<span style="color: #006600;">dataObj</span>;
	<span style="color: #000000; font-weight: bold;">var</span> cmd:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">data</span>._cmd;
	<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>cmd<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;loginKO&quot;</span>:
			<span style="color: #0066CC;">this</span>._labWelcome.<span style="color: #006600;">setText</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #006600;">err</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;loginOK&quot;</span>:
			_SFSClient.<span style="color: #006600;">myUserId</span> = <span style="color: #0066CC;">data</span>.<span style="color: #006600;">id</span>;
			_SFSClient.<span style="color: #006600;">myUserName</span> = <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">name</span>;
			initResources<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #000000; font-weight: bold;">default</span>:
			<span style="color: #0066CC;">this</span>._labWelcome.<span style="color: #006600;">setText</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span>.<span style="color: #006600;">err</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

</li>
</ol>
<h3 class="related_post_title">你可能还对下面的日志感兴趣：</h3>
<ul class="related_post">
<li><a href="http://junnan.org/blog/?p=1027" title="用数据库读取和存储Smartfoxserver的好友列表(BuddyList)">用数据库读取和存储Smartfoxserver的好友列表(BuddyList) (15)</a></li>
<li><a href="http://junnan.org/blog/?p=1033" title="用XMLSocket获得SmartFoxServer的zone在线人数">用XMLSocket获得SmartFoxServer的zone在线人数 (1)</a></li>
<li><a href="http://junnan.org/blog/?p=1147" title="[AS3]利用ByteArray压缩游戏关卡数据">[AS3]利用ByteArray压缩游戏关卡数据 (0)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://junnan.org/blog/?feed=rss2&amp;p=994</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
