﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-cslover-GameFans</title><link>http://www.cppblog.com/cslover/</link><description>热爱游戏开发，专注于手游开发</description><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:06:49 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:06:49 GMT</pubDate><ttl>60</ttl><item><title>在Cocos2d-x中创建自己的项目工程</title><link>http://www.cppblog.com/cslover/archive/2014/01/01/205104.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Wed, 01 Jan 2014 15:27:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2014/01/01/205104.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/205104.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2014/01/01/205104.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/205104.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/205104.html</trackback:ping><description><![CDATA[本人用的是VS2010，Cocos2d-x 2.2.1<br />向cocos2d-x中添加工程，位置选择cocos2d-x\samples\Cpp在该目录下面，创建工程名为Test236的空工程<br /><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20140101221839.jpg" border="0" alt="" width="432" height="87" /><br />在Test236目录下面添加3个目录分别为Classes，Resources,proj.win32目录，并把刚刚新建的工程移动到proj.win32目录中，这样我们就可以添加文件到工程中去了，copy吧，把HelloWorld中main.h,main.cpp移动到proj.win32目录中，并把AppDelegate.h,AppDelegate.cpp,AppMacros.h移动到Classes目录中，然后就是创建场景的过程了，这里可以继续copy HelloWorld中的HelloWorld.h,HelloWorld.cpp到Classes目录中，然后再工程中一次将源码文件加入到工程中<br />然后编译Test236工程中，发现如下错误：<br /><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20140101223718.jpg" border="0" alt="" width="370" height="96" /><br />这里我们添加cocos2d-x的查找目录在工程-》Property-》Configuration Property-》C/C++ -》AdditionalIncludeDirectories中添加如下查找目录<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;..;<br /><span style="color: #008080; ">&nbsp;2</span>&nbsp;..\Classes;<br /><span style="color: #008080; ">&nbsp;3</span>&nbsp;$(ProjectDir)..\..\..\..\cocos2dx;<br /><span style="color: #008080; ">&nbsp;4</span>&nbsp;$(ProjectDir)..\..\..\..\cocos2dx\include;<br /><span style="color: #008080; ">&nbsp;5</span>&nbsp;$(ProjectDir)..\..\..\..\cocos2dx\kazmath\include;<br /><span style="color: #008080; ">&nbsp;6</span>&nbsp;$(ProjectDir)..\..\..\..\cocos2dx\platform\win32;<br /><span style="color: #008080; ">&nbsp;7</span>&nbsp;$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32;<br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32\OGLES;<br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;$(ProjectDir)..\..\..\..\external;<br /><span style="color: #008080; ">10</span>&nbsp;$(ProjectDir)..\..\..\..\external\chipmunk\include\chipmunk;<br /><span style="color: #008080; ">11</span>&nbsp;$(ProjectDir)..\..\..\..\CocosDenshion\include;</div><div><span style="font-size: 13px; color: #008080;">12</span><span style="background-color: #eeeeee; font-size: 13px;">&nbsp;</span><span style="background-color: #eeeeee; font-size: 13px;">$(ProjectDir)..\..\..\..\extensions;</span>再次编译出现以下错误：<br /><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20140101224924.jpg" border="0" alt="" width="610" height="95" /><br />这里 工程-》Property-》Configuration Property-》C/C++ -》Preprocesser-》Preprocesser Definitions添加<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->WIN32;_DEBUG;_WINDOWS;_USE_MATH_DEFINES;GL_GLEXT_PROTOTYPES;CC_ENABLE_CHIPMUNK_INTEGRATION=1;<br />COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;</div>继续编译错误如下：<br /><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20140101225611.jpg" border="0" alt="" width="696" height="149" /><br />工程-》Property-》Configuration Property-》Linker-》Input-》AdditionalDependencies添加<br /><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->libcocos2d.lib;libCocosDenshion.lib;opengl32.lib;glew32.lib;libBox2d.lib;libchipmunk.lib;websockets.lib;</div>编译出错如下：<br /><div>fatal error LNK1104: cannot open file 'libcocos2d.lib'</div>工程-》Property-》Configuration Property-》Linker-》General-》AdditionalLibraryDirectories<br /><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->$(ProjectDir)..\..\..\..\Debug.win32</div>编译通过，运行出错如下<br /><div> <img src="file:///C:\Users\Administrator\AppData\Roaming\Tencent\Users\574197976\QQ\WinTemp\RichOle\BL`QE2MH(~ZT7U`1D_Q~IF5.jpg"  alt="" /></div><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20140101231400.jpg" border="0" alt="" width="378" height="78" /><br />工程-》Property-》Configuration Property-》Debugging-》Working Directory中添加工作目录<br /><div><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->$(ProjectDir)..\Resources</div></div>运行继续出错如下：<br /><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20140101231949.jpg" border="0" alt="" width="438" height="83" /><br /><br />工程-》Property-》Configuration Property-》Debugging-》Environment中添加环境变量<br /><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->PATH=$(ProjectDir)..\..\..\..\Debug.win32</div>同时修改工程为Unicode编码方式。运行正常了<br />Resource是目录有有三个文件分别为HelloWorld中的资源<br /><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20140101232457.jpg" border="0" alt="" width="350" height="131" /></div><img src ="http://www.cppblog.com/cslover/aggbug/205104.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2014-01-01 23:27 <a href="http://www.cppblog.com/cslover/archive/2014/01/01/205104.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>AppDelegate中的cocos2d-x 的初始化场景applicationDidFinishLaunching</title><link>http://www.cppblog.com/cslover/archive/2013/12/30/205065.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Mon, 30 Dec 2013 09:23:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/30/205065.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/205065.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/30/205065.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/205065.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/205065.html</trackback:ping><description><![CDATA[<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->&nbsp; &nbsp; CCConfiguration::sharedConfiguration()-&gt;loadConfigFile("configs/config-example.plist");<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;initialize&nbsp;director</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;CCDirector&nbsp;*pDirector&nbsp;=&nbsp;CCDirector::sharedDirector();<br />
&nbsp;&nbsp;&nbsp;&nbsp;pDirector-&gt;setOpenGLView(CCEGLView::sharedOpenGLView());<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;CCSize&nbsp;screenSize&nbsp;=&nbsp;CCEGLView::sharedOpenGLView()-&gt;getFrameSize();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;CCSize&nbsp;designSize&nbsp;=&nbsp;CCSizeMake(480,&nbsp;320);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;CCFileUtils*&nbsp;pFileUtils&nbsp;=&nbsp;CCFileUtils::sharedFileUtils();<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::vector&lt;std::<span style="color: #0000FF; ">string</span>&gt;&nbsp;searchPaths;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(screenSize.height&nbsp;&gt;&nbsp;320)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CCSize&nbsp;resourceSize&nbsp;=&nbsp;CCSizeMake(960,&nbsp;640);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchPaths.push_back("hd");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchPaths.push_back("hd/scenetest");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDirector-&gt;setContentScaleFactor(resourceSize.height/designSize.height);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchPaths.push_back("scenetest");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;pFileUtils-&gt;setSearchPaths(searchPaths);<br />
<br />
<span style="color: #0000FF; ">#if</span>&nbsp;(CC_TARGET_PLATFORM&nbsp;==&nbsp;CC_PLATFORM_WINRT)&nbsp;||&nbsp;(CC_TARGET_PLATFORM&nbsp;==&nbsp;CC_PLATFORM_WP8)<br />
&nbsp;&nbsp;&nbsp;&nbsp;CCEGLView::sharedOpenGLView()-&gt;setDesignResolutionSize(designSize.width,&nbsp;designSize.height,&nbsp;kResolutionShowAll);<br />
<span style="color: #0000FF; ">#else</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;CCEGLView::sharedOpenGLView()-&gt;setDesignResolutionSize(designSize.width,&nbsp;designSize.height,&nbsp;kResolutionNoBorder);<br />
<span style="color: #0000FF; ">#endif</span><br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;CCScene&nbsp;*&nbsp;pScene&nbsp;=&nbsp;CCScene::create();<br />
&nbsp;&nbsp;&nbsp;&nbsp;CCLayer&nbsp;*&nbsp;pLayer&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;TestController();<br />
&nbsp;&nbsp;&nbsp;&nbsp;pLayer-&gt;autorelease();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;pScene-&gt;addChild(pLayer);<br />
&nbsp;&nbsp;&nbsp;&nbsp;pDirector-&gt;runWithScene(pScene);</div>
<br />
<span style="font-size: 14pt;">首先加载程序配置文件plist，然后我们初始化</span><span style="font-size: 14pt; background-color: #eeeeee;">CCDirector导演类，并设置opengl视图环境，根据屏幕宽高比例来决定是否设置查找路径目录，然后创建了一个场景和一个层</span><span style="font-size: 14pt;">它们分别为</span><span style="font-size: 14pt; background-color: #eeeeee;">pScene和</span><span style="font-size: 14pt; background-color: #eeeeee;">pLayer ，并把</span><span style="font-size: 14pt; background-color: #eeeeee;">pLayer 加入到场景中，接着由导演赖决定场景如何演绎，如何播放</span><span style="font-size: 14pt; background-color: #eeeeee;">pDirector</span><span style="font-size: 14pt; background-color: #eeeeee;">-&gt;</span><span style="font-size: 14pt; background-color: #eeeeee;">runWithScene(pScene)，程序中场景如何演绎，如何变化，都将在场景里处理。</span><img src ="http://www.cppblog.com/cslover/aggbug/205065.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-30 17:23 <a href="http://www.cppblog.com/cslover/archive/2013/12/30/205065.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[看图说话]在VMware Workstation 9中安装Mac OS X 10.8 Mountain Lion</title><link>http://www.cppblog.com/cslover/archive/2013/12/29/205058.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sun, 29 Dec 2013 09:33:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/29/205058.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/205058.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/29/205058.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/205058.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/205058.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转载自 &nbsp;&nbsp;http://www.cnblogs.com/cndavidwang/p/3307837.html本文环境： CPU：Intel Core i7 920； OS：Windows 7； 内存：8G；　　玩Hackintosh各有各的理由，不管什么理由，利用虚拟机安装Mac OS X都是一个可行的办法。一、硬件准备　　首先，你要有一台PC机，CPU老一点不是问题，只要支...&nbsp;&nbsp;<a href='http://www.cppblog.com/cslover/archive/2013/12/29/205058.html'>阅读全文</a><img src ="http://www.cppblog.com/cslover/aggbug/205058.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-29 17:33 <a href="http://www.cppblog.com/cslover/archive/2013/12/29/205058.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>cocos2d-x中ignoreAnchorPointForPosition函数</title><link>http://www.cppblog.com/cslover/archive/2013/12/29/205057.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sun, 29 Dec 2013 07:02:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/29/205057.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/205057.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/29/205057.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/205057.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/205057.html</trackback:ping><description><![CDATA[<div>cocos2d中<span style="color: #ff0000;">CCLayerColor&nbsp;</span>的默认锚点是(0.5,0.5)。该节点的坐标是参考父节点的，而父节点的坐标原点是父节点left、buttom。</div><div><span style="white-space: pre; color: red;">	</span><span style="color: red;">CCLayerColor *layer1 = CCLayerColor::create(ccc4(255,0,0,255),200,200);</span></div><div><span style="white-space:pre">	</span><span style="color: red;">CCLayerColor *layer2 = CCLayerColor::create(ccc4(0,255,0,255),100,100);</span></div><div><span style="white-space:pre">	</span><span style="color: red;">this-&gt;addChild(layer1);</span></div><div><span style="white-space:pre">	</span><span style="color: red;">layer1-&gt;addChild(layer2);</span></div><div>这里我们添加了2个层，而layer1作为layer2父节点，在我们未设置layer2坐标的情况下默认为layer的(left,bottom)效果如下图：<br /><img src="http://www.cppblog.com/images/cppblog_com/cslover/Demo测试/QQ图片20131229143922.jpg" border="0" alt="" width="532" height="408" /><br /><br /><div><span style="white-space: pre; color: red;">	</span><span style="color: red;">CCLayerColor *layer1 = CCLayerColor::create(ccc4(255,0,0,255),200,200);</span></div><div><span style="white-space: pre;">	</span><span style="color: red;">CCLayerColor *layer2 = CCLayerColor::create(ccc4(0,255,0,255),100,100);</span></div><div>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: red;">layer2-&gt;ignoreAnchorPointForPosition(false);</span><span style="white-space: pre;">	</span><br />&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red;">this-&gt;addChild(layer1);</span></div><div><span style="white-space: pre;">	</span><span style="color: red;">layer1-&gt;addChild(layer2);</span></div><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20131229144203.jpg" border="0" alt="" width="526" height="408" /><br /><br />我们默认的position是左下角点的坐标(left,bottom)这里是(0,0)点，即layer1和layer2的position都是(0,0),在开启锚点有效后，<br />我们看出layer2中的正方形只有上图中的1/4,其余部分都越出屏幕区域，这里我们可知layer2的默认锚点为(0.5,0.5),这个锚点就是<br />相对点的意思，比如<span style="color: #ff0000;">LEFT_TOP </span><span>指出了当前CCNode的左上角点对应于</span>CCNode的position点，这像一个映射一般。<br /><div></div><div><span style="white-space: pre; color: red;">		</span><span style="color: red;">var Anchor = {}; &nbsp;</span></div><div><span style="white-space:pre">		</span><span style="color: red;">Anchor.LEFT_TOP = cc.p(0,1); &nbsp;</span></div><div><span style="white-space:pre">		</span><span style="color: red;">Anchor.RIGHT_TOP = cc.p(1, 1); &nbsp;</span></div><div><span style="white-space:pre">		</span><span style="color: red;">Anchor.RIGHT_BOTTOM = cc.p(1, 0); &nbsp;</span></div><div><span style="white-space:pre">		</span><span style="color: red;">Anchor.LEFT_BOTTOM = cc.p(0, 0); &nbsp;</span></div><div><span style="white-space:pre">		</span><span style="color: red;">Anchor.MIDDLE = cc.p(0.5, 0.5);&nbsp;<br /></span></div><div><span style="white-space:pre">下面再贴一段代码和效果图以供理解	<br /></span><span style="color: #ff0000;">&nbsp; &nbsp; &nbsp; &nbsp; CCLayerColor *layer1 = CCLayerColor::create(ccc4(255,0,0,255),200,200);</span></div><div><span style="white-space:pre">	</span><span style="color: #ff0000;">CCLayerColor *layer2 = CCLayerColor::create(ccc4(0,255,0,255),100,100);</span></div><div><span style="white-space:pre">	</span><span style="color: #ff0000;">layer1-&gt;setPosition(ccp(100,100));</span></div><div><span style="white-space:pre">	</span><span style="color: #ff0000;">layer2-&gt;ignoreAnchorPointForPosition(false);</span></div><div><span style="white-space:pre">	</span><span style="color: #ff0000;">this-&gt;addChild(layer1);</span></div><div><span style="white-space:pre">	</span><span style="color: #ff0000;">layer1-&gt;addChild(layer2);</span></div><img src="http://www.cppblog.com/images/cppblog_com/cslover/QQ图片20131229150111.jpg" border="0" alt="" width="668" height="653" /><br /><br /><br /></div><img src ="http://www.cppblog.com/cslover/aggbug/205057.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-29 15:02 <a href="http://www.cppblog.com/cslover/archive/2013/12/29/205057.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>lua Require函数</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204934.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:42:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204934.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204934.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204934.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204934.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204934.html</trackback:ping><description><![CDATA[<p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">Lua<span style="font-family: 宋体;">提供高级的</span>require<span style="font-family: 宋体;">函数来加载运行库。粗略的说</span>require<span style="font-family: 宋体;">和</span>dofile<span style="font-family: 宋体;">完成同样的功能但有两点不同：</span></p><p style="margin: 3pt 0cm 4pt 39pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: -18pt; line-height: 20px;">1.<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;</span>require<span style="font-family: 宋体;">会搜索目录加载文件</span></p><p style="margin: 3pt 0cm 4pt 39pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: -18pt; line-height: 20px;">2.<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;</span>require<span style="font-family: 宋体;">会判断是否文件已经加载避免重复加载同一文件。由于上述特征，</span>require<span style="font-family: 宋体;">在</span>Lua<span style="font-family: 宋体;">中是加载库的更好的函数。</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">require<span style="font-family: 宋体;">使用的路径和普通我们看到的路径还有些区别，我们一般见到的路径都是一个目录列表。</span>require<span style="font-family: 宋体;">的路径是一个模式列表，每一个模式指明一种由虚文件名<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体;">（</span>require<span style="font-family: 宋体;">的参数）转成实文件名的方法。更明确地说，每一个模式是一个包含可选的问号的文件名。匹配的时候</span>Lua<span style="font-family: 宋体;">会首先将问号用虚文件名替换，然后看是否有<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体;">这样的文件存在。如果不存在继续用同样的方法用第二个模式匹配。例如，路径如下：</span></p><div style="line-height: 20px; font-size: 12px; font-family: Simsun; border: 1pt dashed windowtext; padding: 1pt 4pt; background-color: #ffffff; margin-left: 21pt; margin-right: 21pt;"><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #ecf1f4; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">?;?.lua;c:\windows\?;/usr/local/lua/?/?.lua</p></div><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">调用</span>require "lili"<span style="font-family: 宋体;">时会试着打开这些文件：</span></p><div style="line-height: 20px; font-size: 12px; font-family: Simsun; border: 1pt dashed windowtext; padding: 1pt 4pt; background-color: #ffffff; margin-left: 21pt; margin-right: 21pt;"><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #f5e1fb; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">lili</p><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; border: none; padding: 0cm;">lili.lua</p><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #e3f8e4; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">c:\windows\lili</p><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; border: none; padding: 0cm;">/usr/local/lua/lili/lili.lua</p></div><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">require<span style="font-family: 宋体;">关注的问题只有分号（模式之间的分隔符）和问号，其他的信息（目录分隔符，文件扩展名）在路径中定义。</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">为了确定路径，</span>Lua<span style="font-family: 宋体;">首先检查全局变量</span>LUA_PATH<span style="font-family: 宋体;">是否为一个字符串，如果是则认为这个串就是路径；否则</span>require<span style="font-family: 宋体;">检查环境变量</span>LUA_PATH<span style="font-family: 宋体;">的值，如果两个都失败<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;require<span style="font-family: 宋体;">使用固定的路径（典型的</span>"?;?.lua"<span style="font-family: 宋体;">）</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;">require<span style="font-family: 宋体;">的另一个功能是避免重复加载同一个文件两次。</span>Lua<span style="font-family: 宋体;">保留一张所有已经加载的文件的列表（使用</span>table<span style="font-family: 宋体;">保存）。如果一个加载的文件在表中存在</span>require<span style="font-family: 宋体;">简单的返回；<br /></span>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体;">表中保留加载的文件的虚名，而不是实文件名。所以如果你使用不同的虚文件名</span>require<span style="font-family: 宋体;">同一个文件两次，将会加载两次该文件。比如</span>require "foo"<span style="font-family: 宋体;">和</span>require "foo.lua"<span style="font-family: 宋体;">，<br /></span>&nbsp;<span style="font-family: 宋体;">路径为</span>"?;?.lua"<span style="font-family: 宋体;">将会加载</span>foo.lua<span style="font-family: 宋体;">两次。我们也可以通过全局变量</span>_LOADED<span style="font-family: 宋体;">访问文件名列表，这样我们就可以判断文件是否被加载过；同样我们也可以使用一点小技巧让<br /></span>require<span style="font-family: 宋体;">加载一个文件两次。比如，</span>require "foo"<span style="font-family: 宋体;">之后</span>_LOADED["foo"]<span style="font-family: 宋体;">将不为</span>nil<span style="font-family: 宋体;">，我们可以将其赋值为</span>nil<span style="font-family: 宋体;">，</span>require "foo.lua"<span style="font-family: 宋体;">将会再次加载该文件。</span></p><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">一个路径中的模式也可以不包含问号而只是一个固定的路径，比如：</span></p><div style="line-height: 20px; font-size: 12px; font-family: Simsun; border: 1pt dashed windowtext; padding: 1pt 4pt; background-color: #e5ece0; margin-left: 21pt; margin-right: 21pt;"><p style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: 'Courier New'; text-align: justify; line-height: 13pt; background-color: #ffffff; border: none; padding: 0cm; background-position: initial initial; background-repeat: initial initial;">?;?.lua;/usr/local/default.lua</p></div><p style="margin: 3pt 0cm 4pt; font-size: 10.5pt; font-family: 'Times New Roman', serif; text-align: justify; text-indent: 21pt; line-height: 20px;"><span style="font-family: 宋体;">这种情况下，</span>require<span style="font-family: 宋体;">没有匹配的时候就会使用这个固定的文件（当然这个固定的路径必须放在模式列表的最后才有意义）。在</span>require<span style="font-family: 宋体;">运行一个</span>chunk<span style="font-family: 宋体;">以前，它定义了<br />一个全局变量</span>_REQUIREDNAME<span style="font-family: 宋体;">用来保存被</span>required<span style="font-family: 宋体;">的虚文件的文件名。我们可以通过使用这个技巧扩展</span>require<span style="font-family: 宋体;">的功能。举个极端的例子，我们可以把路径设为<br /></span>"/usr/local/lua/newrequire.lua"<span style="font-family: 宋体;">，这样以后每次调用</span>require<span style="font-family: 宋体;">都会运行</span>newrequire.lua<span style="font-family: 宋体;">，这种情况下可以通过使用</span>_REQUIREDNAME<span style="font-family: 宋体;">的值去实际加载</span>required<span style="font-family: 宋体;">的文件。</span></p><img src ="http://www.cppblog.com/cslover/aggbug/204934.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:42 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204934.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> table的函数API</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204932.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:39:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204932.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204932.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204932.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204932.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204932.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 转载自http://dhq.me/lua-learning-notes-tabletable 在 Lua 里是一种重要的数据结构，它可以说是其他数据结构的基础，通常的数组、记录、线性表、队列、集合等数据结构都可以用 table 来表示，甚至连全局变量（_G）、模块、元表（metatable）等这些重要的 Lua 元素都是 table 的结构。可以说，table &nbsp;是一个强大而又神奇的东西...&nbsp;&nbsp;<a href='http://www.cppblog.com/cslover/archive/2013/12/21/204932.html'>阅读全文</a><img src ="http://www.cppblog.com/cslover/aggbug/204932.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:39 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204932.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua库之时间和日期操作</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204931.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:36:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204931.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204931.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204931.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204931.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204931.html</trackback:ping><description><![CDATA[<span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">转载自</span><a href="http://www.cppblog.com/jsunhj/archive/2008/11/28/68082.html">http://www.cppblog.com/jsunhj/archive/2008/11/28/68082.html</a><br /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;"><br />os.time()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;== 返回当前系统的</span><a href="http://www.cppblog.com/jsunhj/archive/2008/11/28/68084.html" style="color: #770000; text-decoration: none; font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">日历时间</a><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">os.date()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;== 返回本地化的时间字符串，这里是"11/28/08 17:23:37"</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">os.date("%x", os.time()) &lt;== 返回自定义格式化时间字符串（</span><a href="http://www.cppblog.com/jsunhj/admin/EditPosts.aspx#FullArgList" style="color: #770000; text-decoration: none; font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">完整的格式化参数</a><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">），这里是"11/28/08"</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">os.clock()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;== 返回执行该程序CPU花去的时钟秒数，这里是1156.726</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><strong style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;"><span style="font-size: 12pt;">附录</span></strong><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><a href="http://www.cppblog.com/jsunhj/admin/EditPosts.aspx#FullArgList" style="color: #770000; text-decoration: none; font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">完整的格式化参数</a><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">这些时间输出的字符串表示是经过本地化的。所以如果是在巴西（葡萄牙语系），"%B" 得到的就是 "setembro"（译者按：大概是葡萄牙语九月？），"%x" 得到的就是 "16/09/98"（月日次序不同）。标记的意义和显示实例总结如下表。实例的时间是在1998年九月16日，星期三，23:48:10。返回值为数字形式的还列出了它们的范围。（都是按照英语系的显示描述的，也比较简单，就不烦了）</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%a abbreviated weekday name (e.g., Wed)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%A full weekday name (e.g., Wednesday)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%b abbreviated month name (e.g., Sep)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%B full month name (e.g., September)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;"><strong>%c</strong>&nbsp;date and time (e.g., 09/16/98 23:48:10)&nbsp;<br /></span><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%d day of the month (16) [01-31]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%H hour, using a 24-hour clock (23) [00-23]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%I hour, using a 12-hour clock (11) [01-12]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%M minute (48) [00-59]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%m month (09) [01-12]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%p either "am" or "pm" (pm)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%S second (10) [00-61]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%w weekday (3) [0-6 = Sunday-Saturday]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%x date (e.g., 09/16/98)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%X time (e.g., 23:48:10)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%Y full year (1998)</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%y two-digit year (98) [00-99]</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">%% the character '%'</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" />事实上如果不使用任何参数就调用date，就是以%c的形式输出。这样就是得到经过格式化的完整时间信息。<br /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">注意%x、%X和%c由所在地区和计算机系统的改变会发生变化。</span><br style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;" /><span style="font-family: 宋体; font-size: 13px; line-height: normal; background-color: #eeeedd;">如果该字符串要确定下来（例如确定为mm/dd/yyyy），可以使用明确的字符串格式方式（例如"%m/%d/%Y"）。</span><img src ="http://www.cppblog.com/cslover/aggbug/204931.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:36 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204931.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua module机制分析</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204928.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:25:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204928.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204928.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204928.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204928.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204928.html</trackback:ping><description><![CDATA[<span style="margin: 0px; padding: 0px; font-size: small; color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;"><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; color: #000000; font-size: 13px; line-height: 19px;"><span style="margin: 0px; padding: 0px;">转载自<a href="http://www.cnblogs.com/orez88/articles/2139160.html">http://www.cnblogs.com/orez88/articles/2139160.html</a><br />Lua</span><span style="margin: 0px; padding: 0px;">中的模块</span><span style="margin: 0px; padding: 0px;">(module)</span><span style="margin: 0px; padding: 0px;">机制有点类似于</span><span style="margin: 0px; padding: 0px;">C</span><span style="margin: 0px; padding: 0px;">语言的动态库，可以将一些公共的代码放在模块中，以</span><span style="margin: 0px; padding: 0px;">API</span><span style="margin: 0px; padding: 0px;">的形式提供给</span><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">应用脚本使用。<br />使用模块或许还可以做成像</span><span style="margin: 0px; padding: 0px;">Android</span><span style="margin: 0px; padding: 0px;">的</span><span style="margin: 0px; padding: 0px;">Application Frameworks</span><span style="margin: 0px; padding: 0px;">，建立一种新的应用程序编程模型，只是猜想。</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; color: #000000; font-size: 13px; line-height: 19px;"><span style="margin: 0px; padding: 0px;">模块最简单的使用方式：</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; color: #000000; font-size: 13px; line-height: 19px;"></p><div bg_javascript"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: transparent; width: 687px; word-break: break-all; word-wrap: break-word;"><div style="margin: 0px;"><div style="margin: 0px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; font-weight: bold; color: black;"><a href="http://blog.csdn.net/chenyufei1013/article/details/4438801#" title="view plain" style="padding: 1px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); border-bottom-style: dotted; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">view plain</a></div></div><ol start="1" style="margin-left: 0px; padding-top: 2px; padding-bottom: 2px; padding-left: 50px; border-style: solid; color: #2b91af;"><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">require&nbsp;&#8220;os&#8221;&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; background-color: #fafafa; line-height: 18px;">os.clock() &nbsp;</li></ol></div></span><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">table</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">语言共有</span><span style="margin: 0px; padding: 0px;">8</span><span style="margin: 0px; padding: 0px;">中数据类型，其中</span><span style="margin: 0px; padding: 0px;">table(</span><span style="margin: 0px; padding: 0px;">表</span><span style="margin: 0px; padding: 0px;">)</span><span style="margin: 0px; padding: 0px;">类型实现了&#8220;关联数组（</span><span style="margin: 0px; padding: 0px;">associative array</span><span style="margin: 0px; padding: 0px;">）&#8221;。</span><span style="margin: 0px; padding: 0px;">table</span><span style="margin: 0px; padding: 0px;">中可以保存各种数据类型。和</span><span style="margin: 0px; padding: 0px;">table</span><span style="margin: 0px; padding: 0px;">关联的<a name="2.8" style="margin: 0px; padding: 0px; color: rgb(51, 102, 153); border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: rgb(51, 51, 51); width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat;"></a></span><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;">metatable</span></span><span style="margin: 0px; padding: 0px;">，<br />它定义了表的操作的一些行为，比如</span><span style="margin: 0px; padding: 0px;">:&nbsp;</span><span style="margin: 0px; padding: 0px;">使用</span><span style="margin: 0px; padding: 0px;">+</span><span style="margin: 0px; padding: 0px;">操作时，会去查找</span><span style="margin: 0px; padding: 0px;">metatable</span><span style="margin: 0px; padding: 0px;">的属性</span><span style="margin: 0px; padding: 0px;">__add</span><span style="margin: 0px; padding: 0px;">，有的话执行。</span></p><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">function</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">function</span><span style="margin: 0px; padding: 0px;">是</span><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">中</span><span style="margin: 0px; padding: 0px;">8</span><span style="margin: 0px; padding: 0px;">中基本类型之一，和其它类型一样可以作为值传递。和</span><span style="margin: 0px; padding: 0px;">function</span><span style="margin: 0px; padding: 0px;">有关联的表是环境表（</span><em style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px;">environment</span></em><span style="margin: 0px; padding: 0px;">），可以认为是</span><span style="margin: 0px; padding: 0px;">function</span><span style="margin: 0px; padding: 0px;">的执行环境。<br />可以通过</span><span style="margin: 0px; padding: 0px;">setfenv()</span><span style="margin: 0px; padding: 0px;">和</span><span style="margin: 0px; padding: 0px;">getfenv()</span><span style="margin: 0px; padding: 0px;">来设置和获取函数的环境表。函数中对于全局的操作，比如：定义全局变量等；就是在这个表中进行的。多个函数可以共享一个表</span></p><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">require</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">当使用</span><span style="margin: 0px; padding: 0px;">require</span><span style="margin: 0px; padding: 0px;">加载一个模块时，实际上做了如下的事情：</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;"></span><span style="margin: 0px; padding: 0px;">在表</span><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中查找模块是否存在，有的话就返回该值。可以使用如下函数输出表中的内容</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"></p><div bg_c-sharp"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: #ffffff; width: 687px; word-break: break-all; word-wrap: break-word;"><div style="margin: 0px;"><div style="margin: 0px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; font-weight: bold;"><a href="http://blog.csdn.net/chenyufei1013/article/details/4438801#" title="view plain" style="padding: 1px; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); border-bottom-style: dotted; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">view plain</a></div></div><ol start="1" style="margin-left: 0px; padding-top: 2px; padding-bottom: 2px; padding-left: 50px; border-style: solid; color: #2b91af;"><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">function&nbsp;walkTable(t)&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; background-color: #fafafa; line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">for</span>&nbsp;k,v&nbsp;<span style="color: #7f0055;">in</span>&nbsp;pairs(t)&nbsp;<span style="color: #7f0055;">do</span>&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("+++",k,v,"+++")&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; background-color: #fafafa; line-height: 18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;&nbsp;</li><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">end &nbsp;</li></ol></div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中的内容如下，其中包含了一些默认加载的库和全局表</span><span style="margin: 0px; padding: 0px;">_G</span><br /></p><div>+++ string &nbsp; &nbsp; &nbsp;, &nbsp; &nbsp; &nbsp;table: 003B82B0 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ debug &nbsp; &nbsp; &nbsp;, &nbsp; &nbsp; &nbsp;table: 003B9300 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ package &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B5C30 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ _G &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B2E10 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ io &nbsp; &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B7320 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ os &nbsp; &nbsp;, &nbsp; &nbsp; &nbsp;table: 003B7988 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ table , &nbsp; &nbsp; &nbsp;table: 003B51E0 &nbsp; &nbsp; +++ &nbsp;</div><div>+++ math &nbsp; &nbsp; &nbsp; , &nbsp; &nbsp; &nbsp;table: 003B8728 &nbsp; &nbsp; &nbsp;+++ &nbsp;</div><div>+++ coroutine , &nbsp; &nbsp; &nbsp;table: 003B59D8 &nbsp; &nbsp; +++ &nbsp;</div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;"><br />为模块查找加载器（</span><span style="margin: 0px; padding: 0px;">loader</span><span style="margin: 0px; padding: 0px;">），查询</span><span style="margin: 0px; padding: 0px;">package.preload</span><span style="margin: 0px; padding: 0px;">，通常为空</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">加载</span><span style="margin: 0px; padding: 0px;">Lua</span><span style="margin: 0px; padding: 0px;">文件使用</span><span style="margin: 0px; padding: 0px;">loadfile</span><span style="margin: 0px; padding: 0px;">函数。</span><span style="margin: 0px; padding: 0px;">loadfile</span><span style="margin: 0px; padding: 0px;">会将文件当作函数来加载，</span><span style="margin: 0px; padding: 0px;">require</span><span style="margin: 0px; padding: 0px;">会将模块名作为参数传给该函数。若有返回值则将返回值放入表</span><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中。<br />若没有则返回表</span><span style="margin: 0px; padding: 0px;">package.loaded</span><span style="margin: 0px; padding: 0px;">中的值。</span></p><ul style="margin: 0px 0px 0px 45px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px; list-style: inherit !important;"><strong style="margin: 0px; padding: 0px;">module</strong></li></ul><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">当在模块文件中使用</span><span style="margin: 0px; padding: 0px;">module</span><span style="margin: 0px; padding: 0px;">函数的时候，如下所示；</span></p><div bg_c-sharp"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: #ffffff; width: 687px; word-break: break-all; word-wrap: break-word;"><ol start="1" style="margin-left: 0px; padding-top: 2px; padding-bottom: 2px; padding-left: 50px; border-style: solid; color: #2b91af;"><li style="margin-left: 38px; list-style-type: decimal; line-height: 18px;">module&nbsp;&#8220;mymodule&#8221; &nbsp;</li></ol></div><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"><span style="margin: 0px; padding: 0px;">实际上等同于以下的语句：</span></p><p style="margin-top: 10px; margin-bottom: 10px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff; font-size: small; line-height: 19px;"></p><div bg_c-sharp"="" style="margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; font-family: Consolas, 'Courier New', Courier, mono; background-color: #ffffff; width: 687px; word-break: break-all; word-wrap: break-word;"><div style="margin: 0px;"><div style="margin: 0px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; font-weight: bold;"><div>local modname = &#8220;mymodule&#8221; &nbsp; &nbsp; &#8211; 定义模块名 &nbsp;</div><div>local M = {} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- 定义用于返回的模块表 &nbsp;</div><div>_G[modname] = M &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- 将模块表加入到全局变量中 &nbsp;</div><div>package.loaded[modname] = M &nbsp; &nbsp;-- 将模块表加入到package.loaded中，防止多次加载 &nbsp;</div><div>setfenv(1,M) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- 将模块表设置为函数的环境表，这使得模块中的所有操作是以在模块表中的，这样定义函数就直接定义在模块表中&nbsp;</div></div></div></div>&nbsp;<span style="margin: 0px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;">通过</span><span style="margin: 0px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;">module()</span><span style="margin: 0px; padding: 0px; font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; font-size: small; line-height: 23px; background-color: #ffffff;">，可以方便的编写模块中的内容。</span><img src ="http://www.cppblog.com/cslover/aggbug/204928.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:25 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204928.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>解析LUA中关于Package定义</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204925.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:14:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204925.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204925.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204925.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204925.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204925.html</trackback:ping><description><![CDATA[<p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;"><strong>转载自</strong><a href="http://mobile.51cto.com/iphone-285706.htm">http://mobile.51cto.com/iphone-285706.htm</a><br /><strong>LUA</strong>中关于<strong>Package</strong>定义学习笔记是本文要介绍的内容，主要是来学习<strong>lua</strong>中对<strong>Package</strong>，具体内容俩看本文详解。<strong>Package</strong>是个很逻辑的概念。也就是说很可能不是语言直接提供的设施。cpp里的namespace可以用来实现<strong>package</strong>，在<strong>LUA</strong>中则是依靠table机制来实现。实现的方式大同小异，本文总结了在《<strong>lua</strong>中文手册》中出现的3种方式。</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">方式一: 直接将所有组成包的数据和方法定义在包中，代码看起来像是这样：</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">vector3d</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;{}&nbsp;&nbsp;--&nbsp;包名 &nbsp;</span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">function&nbsp;vector3d.function1() &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">function&nbsp;vector3d.function2() &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(vector3d.function1())&nbsp;then &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...... &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">return&nbsp;vector3d&nbsp;</span></li></ol></pre><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">这样定义的就是一个vector3d包，使用require语言打开这个包后，就可以使用 vector3d.function1和vector3d.function2这两个函数了。</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">这是最直接最好理解的一种Package定义方式，但是有一定的弊端。这个弊端主要体现在Package的实现过程中。可以看到，即使在</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">vector3d.function2()中使用function1()函数，也必须完整的加上vector3d包名，否则无法进行函数调用。Package的作者要稍微累一点，不过使用者倒是还好。特别的注意最后的 return vector3d 语句，有了这句后调用者可以按照如下方式重命名包:</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">MyPackage</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">require</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;"vector3d" &nbsp;</span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">MyPackage.function2()&nbsp;</span></li></ol></pre><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">方式二: 使用局部函数定义所有的<strong>Package</strong>内函数，然后在Package的结尾处将需要公开的函数直接放入Package中。代码看起来像这样:</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">vector3d</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">&nbsp;=&nbsp;{}&nbsp;&nbsp;--&nbsp;包名 &nbsp;</span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">local&nbsp;function&nbsp;function1() &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;">&nbsp;</li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">local&nbsp;function&nbsp;function2() &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(function1())&nbsp;then &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...... &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">vector3d</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;=&nbsp;{</span><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">function1</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;=&nbsp;</span><span style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">functoin1</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">,&nbsp; &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">function2</span><span style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">function2</span><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;=&nbsp;function2 &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">} &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">return&nbsp;vector3d&nbsp;</span></li></ol></pre><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">最后给包中赋值的部分就是将需要的接口公开的部分。这样做的好处：不需要公开的函数可以完全隐藏起来(都是local函数)；Package内部的各个函数相互之间调用的时候不再需要加Package名称进行区分; 可以按照需要随意的重命名Package公开的接口名称。这种方式的弊端在于定义的时候需要写上local，这算不算弊端就看你了 - -。 就我个人而言，我还是比较喜欢这个定义方式。能明确的区分出接口和私有的定义，公开接口的名称还可以随意改变，这就意味着可以随意替换内部实现而不需要影响外部调用者。</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">无论用什么方法去定义Package，都是为了在逻辑上更好的规划代码层次。<strong>LUA</strong>中的table机制的确是一个活力无限的机制啊。Package依靠这个实现，<strong>LUA</strong>本身自己有些机制也依赖于Table(比如全局变量就放在_G表中)</p><p style="margin: 10px 0px; padding: 0px; color: #333333; background-color: #f8f8f8; text-indent: 28px; font-family: 宋体; line-height: 28px;">手册里还有个如何"拆开"<strong>Package</strong>的代码段，很好的展现了table的强大之处(Package也是在table上构筑的逻辑产物)。将Package拆开的意思，就是将所有 Package中公开的名字放入_G表中。也就是让 Package.A() 变成_G.A&nbsp; (_G在一般情况下不需要写，默认引用了)</p><pre style="margin-top: 0px; padding: 0px; color: #333333; font-family: 'Courier New', monospace; font-size: 12px; width: 591.015625px; overflow: auto; background-color: #e6e6e6; line-height: 28px;"><ol style="margin-left: 55px; padding: 5px 0px; color: #5c5c5c; list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: #f7f7f7; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">function&nbsp;openpackage&nbsp;(ns) &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;n,v&nbsp;in&nbsp;pairs(ns)&nbsp; &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_G[n]&nbsp;=&nbsp;v &nbsp;</span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;</span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">end&nbsp;<br /></span></li></ol></pre><img src ="http://www.cppblog.com/cslover/aggbug/204925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:14 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Lua中xpcall函数</title><link>http://www.cppblog.com/cslover/archive/2013/12/21/204924.html</link><dc:creator>cslover</dc:creator><author>cslover</author><pubDate>Sat, 21 Dec 2013 09:03:00 GMT</pubDate><guid>http://www.cppblog.com/cslover/archive/2013/12/21/204924.html</guid><wfw:comment>http://www.cppblog.com/cslover/comments/204924.html</wfw:comment><comments>http://www.cppblog.com/cslover/archive/2013/12/21/204924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cslover/comments/commentRss/204924.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cslover/services/trackbacks/204924.html</trackback:ping><description><![CDATA[<span style="background-color: #ffffff; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px;">Lua提供了xpcall来实现这个功能，xpcall接受两个参数：调用函数、错误处理函数。当错误发生时，Lua会在栈释放以前调用错误处理函数，因此可以使用debug库收集错误相关信息。<br />有两个常用的debug处理函数：debug.debug和debug.traceback，前者给出Lua的提示符，你可以自己动手察看错误发生时的情况；后者通过traceback创建更多的错误信息，也是控制台解释器用来构建错误信息的函数。<br />你可以在任何时候调用debug.traceback获取当前运行的traceback信息：</span><br style="background-color: #ffffff; padding: 0px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px;" /><span style="background-color: #ffffff; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px;">print(debug.traceback())<br /></span><img src ="http://www.cppblog.com/cslover/aggbug/204924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cslover/" target="_blank">cslover</a> 2013-12-21 17:03 <a href="http://www.cppblog.com/cslover/archive/2013/12/21/204924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>