<?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>Music Sets Me Free</title>
	<atom:link href="http://www.bennychen.cn/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bennychen.cn</link>
	<description>Whatever Keeps Situation Sharp</description>
	<lastBuildDate>Tue, 04 Oct 2011 03:44:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Unity Developer ++</title>
		<link>http://www.bennychen.cn/2011/10/unity-developer/</link>
		<comments>http://www.bennychen.cn/2011/10/unity-developer/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 03:44:56 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Unity]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[unity]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1682</guid>
		<description><![CDATA[去新公司工作已经有一个月了。通过这一个月时间的努力，使得这个世界上又多了一位Unity Developer &#8211; ME! 这一个月围绕Unity 3D引擎基本上做了这样一些事情： 前两个星期用Unity完成了一个基本的demo； 利用Unity的shader system完成物体的silhouette outlined effect，目前效果已经实现，但是在算法的优化和实现上还有一些问题。在国庆前给Unity公司的Ole发了邮件请求帮助算法的优化，Ole告诉我有解，但因为正忙于一年一度的Unite Conference，所以还没有时间给我详细回复。稍后应该会有一篇文章描述更多的实现细节; 基于Unity的GUI system，完成了一些GUI animation，最重要的是实现了方便GUI animation的一些Utility class &#8211; Sprite2D, Sprite2DGroup。 另外的一些事情： 开始使用Evernote，并且马上爱上它，太赞了！ 仍在挣扎适应公司同事不同国家的英文口音 >_< 公司的五人足球队成立了，并且在第一场对英国大使馆的比赛中，我上演了帽子戏法(^^) PS:今早不到10点就跑到了中关村的Starbucks，享受包场=.=]]></description>
			<content:encoded><![CDATA[<p>去新公司工作已经有一个月了。通过这一个月时间的努力，使得这个世界上又多了一位Unity Developer &#8211; ME!</p>
<p>这一个月围绕Unity 3D引擎基本上做了这样一些事情：</p>
<ul>
<li>
前两个星期用Unity完成了一个基本的demo；
</li>
<li>
利用Unity的shader system完成物体的silhouette outlined effect，目前效果已经实现，但是在算法的优化和实现上还有一些问题。在国庆前给Unity公司的Ole发了邮件请求帮助算法的优化，Ole告诉我有解，但因为正忙于一年一度的Unite Conference，所以还没有时间给我详细回复。稍后应该会有一篇文章描述更多的实现细节;
</li>
<li>
基于Unity的GUI system，完成了一些GUI animation，最重要的是实现了方便GUI animation的一些Utility class &#8211; Sprite2D, Sprite2DGroup。</ol>
</li>
</ul>
<p>另外的一些事情：</p>
<ul>
<li>开始使用Evernote，并且马上爱上它，太赞了！</li>
<li>仍在挣扎适应公司同事不同国家的英文口音 >_< </li>
<li>公司的五人足球队成立了，并且在第一场对英国大使馆的比赛中，我上演了帽子戏法(^^)</li>
</ul>
<p>PS:今早不到10点就跑到了中关村的Starbucks，享受包场=.=</p>
<p><a href="http://www.bennychen.cn/wp-content/uploads/2011/10/3f82386djw1dlrt2f9syzj.jpg" class="highslide-image" onclick="return hs.expand(this);"><img src="http://www.bennychen.cn/wp-content/uploads/2011/10/3f82386djw1dlrt2f9syzj-300x300.jpg" alt="" title="zhongguancun_starbucks" width="300" height="300" class="alignnone size-medium wp-image-1683" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/10/unity-developer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another Thread @ Moai Forum</title>
		<link>http://www.bennychen.cn/2011/10/another-thread-moai-forum/</link>
		<comments>http://www.bennychen.cn/2011/10/another-thread-moai-forum/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 03:07:48 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[callWithDelay]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[Moai]]></category>
		<category><![CDATA[setTimeout]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1677</guid>
		<description><![CDATA[Does Moai support an API like &#8216;setTimeout&#8217;? http://getmoai.com/forums/moai-sdk-developer-support/does-moai-support-an-api-like-settimeout/ Me Hi, Does Moai support an API like JavaScript&#8217;s setTimeout, which schedules a function execution after a specified time. I noticed there is a Moai class called MOAITimer, so I&#8217;m wondering if this class could be used for this purpose? I know a &#8216;setTimeout&#8217; function could be [...]]]></description>
			<content:encoded><![CDATA[<h3>Does Moai support an API like &#8216;setTimeout&#8217;?</h3>
<p><a href="http://getmoai.com/forums/moai-sdk-developer-support/does-moai-support-an-api-like-settimeout/" target="_blank">http://getmoai.com/forums/moai-sdk-developer-support/does-moai-support-an-api-like-settimeout/</a></p>
<p><strong>Me</strong><br />
Hi,<br />
Does Moai support an API like JavaScript&#8217;s setTimeout, which schedules a function execution after a specified time. I noticed there is a Moai class called MOAITimer, so I&#8217;m wondering if this class could be used for this purpose? I know a &#8216;setTimeout&#8217; function could be easily realized on Lua level using MOAISim:getElapsedTime() and Lua&#8217;s &#8216;pcall&#8217; function, but I&#8217;m just lazy and wonder if there is a shortcut that Moai has provided?<br />
Thank you~</p>
<p><strong>Josh</strong><br />
Moai doesn&#8217;t support that, but for convenience I&#8217;ll share the Lua function I use for this exact purpose:<br />
(Hide some code)<br />
EDIT: updated while investigating bug mentioned below. Will ensure this code works once bug is fixed.</p>
<p><strong>Me</strong><br />
Thank you for the sharing, but the function gives the following error after I run:</p>
<blockquote><p>cannot use &#8216;…&#8217; outside a vararg function near &#8216;…&#8217;</p></blockquote>
<p>I think &#8216;…&#8217; are also needed for &#8220;function()&#8221; in the 5th line, so I added it.<br />
However, after I ran the following code</p>
<pre class="brush: lua; title: ; notranslate">
function printAStr( str )
    print( str )
end
callWithDelay( 1, printAStr, &quot;helloWorld&quot; )
</pre>
<p>It prints &#8220;userdata: 0x3192c4&#8243; instead of &#8220;helloWorld&#8221;, any clue for this? I think the core issue is how to pass arguments to the callback function of timer&#8217;s listener?</p>
<p><strong>Josh</strong><br />
I made two mistakes in my function, which I have corrected above. You are correct I forgot to pass the … properly, and I also forgot that the timer listener callback receives the timer as the first parameter, which is why you were seeing the userdata print out in your test.</p>
<p><strong>Me</strong><br />
seems still unworkable for the arguments pass…<br />
Ok, so my question is how to pass arguments to callback function of timer&#8217;s listener?</p>
<p><strong>Josh</strong><br />
It looks like there may be a bug in the listener with the last release. I&#8217;ve opened a bug on the issue. I also realized yet another error in my above code, and have corrected it. Hopefully that&#8217;s the last edit I have to do. <img src='http://www.bennychen.cn/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><strong>Patrick</strong><br />
Thanks for finding the bug. Won&#8217;t be able to get to it for a day or so. Sorry for the trouble!</p>
<p><strong>Patrick</strong><br />
OK, finally had time to look at this. It looks like it&#8217;s working correctly. The code I used is below.</p>
<pre class="brush: lua; title: ; notranslate">
MOAISim.openWindow ( &quot;test&quot;, 320, 480 )
function callWithDelay ( delay, func, ... )
  local timer = MOAITimer.new ()
  timer:setSpan ( delay )
  timer:setListener ( MOAITimer.EVENT_TIMER_LOOP,
    function ()
      timer:stop ()
      timer = nil
      func ( unpack ( arg ))
    end
  )
  timer:start ()
end

function printAStr ( str ) print( str ) end
callWithDelay ( 1, printAStr, &quot;helloWorld&quot; )
</pre>
<p><strong>Me</strong><br />
It works! Thanks a lot <img src='http://www.bennychen.cn/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/10/another-thread-moai-forum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>1st Day of Golden Week</title>
		<link>http://www.bennychen.cn/2011/10/1st-day-of-golden-week/</link>
		<comments>http://www.bennychen.cn/2011/10/1st-day-of-golden-week/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 15:51:39 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[Moai]]></category>
		<category><![CDATA[pacman]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1673</guid>
		<description><![CDATA[黄金周第一天，将之前开发的Pacman移植到了iPad上。因为Moai引擎，移植变得很容易，只需要把input层做一些修改。我的第一个iOS上的game demo就此诞生 已经将代码更新到了github &#8211; https://github.com/bennychen/Moai-based-Pacman]]></description>
			<content:encoded><![CDATA[<p>黄金周第一天，将之前开发的Pacman移植到了iPad上。因为Moai引擎，移植变得很容易，只需要把input层做一些修改。我的第一个iOS上的game demo就此诞生 <img src='http://www.bennychen.cn/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>已经将代码更新到了github &#8211; <a href="https://github.com/bennychen/Moai-based-Pacman">https://github.com/bennychen/Moai-based-Pacman</a></p>
<p><a href="http://www.bennychen.cn/wp-content/uploads/2011/10/IMAG0428.jpg" class="highslide-image" onclick="return hs.expand(this);"><img src="http://www.bennychen.cn/wp-content/uploads/2011/10/IMAG0428-300x179.jpg" alt="" title="ipad_pacman_moai" width="300" height="179" class="alignnone size-medium wp-image-1674" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/10/1st-day-of-golden-week/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>为SyntaxHighlighter添加新语言</title>
		<link>http://www.bennychen.cn/2011/09/add-new-brush-for-syntaxhighlighter/</link>
		<comments>http://www.bennychen.cn/2011/09/add-new-brush-for-syntaxhighlighter/#comments</comments>
		<pubDate>Sun, 04 Sep 2011 05:36:03 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Some Experiences]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[new brush]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[syntaxhighlighter]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[新语言]]></category>
		<category><![CDATA[添加]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1654</guid>
		<description><![CDATA[因为经常要在博客里贴一些Lua代码，但是所使用的SyntaxHighlighter插件默认不支持Lua语言，所以去研究了一下如何为SyntaxHighlighter添加并激活一个新的语言，这里将过程和有同样需求的童鞋分享。（因为我添加的是Lua语言，下面的过程描述会以Lua为例，在添加你所需要的语言时，你只要将相应的项更换为你的自定义设置即可） 1. 从这篇博客里寻找所需要的语言：http://www.undermyhat.org/blog/2009/09/list-of-brushes-syntaxhighligher/； 2. 下载对应的shBrushXXX.js脚本，比如我下载的是shBrushLua.js，它看起来像这样： 3. 使用FTP工具登陆到WordPress空间，进入到wp-content/plugins目录，新建一个目录，取一个有意义的名字，比如syntaxhighlighter-lua； 4. 将shBrushLua.js上传到新创建的目录； 5. 在该目录创建一个另一个shBrushLua.php文件，添加如下内容： 6. 文件都准备完了，OK，进入到WordPress后台管理的Plugins下，应该能看到新添加的一项syntaxhighlighter-lua，激活它。 Done! It should work now! 其实新添加的js和php文件也可以放到SyntaxHighlighter插件本身的目录下，但是让它独立成插件的好处是，当SyntaxHighlighter升级时，你的个人配置不会因为覆盖而丢失。]]></description>
			<content:encoded><![CDATA[<p>因为经常要在博客里贴一些Lua代码，但是所使用的SyntaxHighlighter插件默认不支持Lua语言，所以去研究了一下如何为SyntaxHighlighter添加并激活一个新的语言，这里将过程和有同样需求的童鞋分享。（因为我添加的是Lua语言，下面的过程描述会以Lua为例，在添加你所需要的语言时，你只要将相应的项更换为你的自定义设置即可）</p>
<p>1. 从这篇博客里寻找所需要的语言：<a href="http://www.undermyhat.org/blog/2009/09/list-of-brushes-syntaxhighligher/">http://www.undermyhat.org/blog/2009/09/list-of-brushes-syntaxhighligher/</a>；<br />
2. 下载对应的shBrushXXX.js脚本，比如我下载的是shBrushLua.js，它看起来像这样：</p>
<pre class="brush: jscript; collapse: true; light: false; title: ; toolbar: true; notranslate">
SyntaxHighlighter.brushes.Lua = function()
{
	var keywords =	'break do end else elseif function if local nil not or repeat return and then until while this';
	var funcs = 'math\\.\\w+ string\\.\\w+ os\\.\\w+ debug\\.\\w+ io\\.\\w+ error fopen dofile coroutine\\.\\w+ arg getmetatable ipairs loadfile loadlib loadstring longjmp print rawget rawset seek setmetatable assert tonumber tostring';

	this.regexList = [
		{ regex: new RegExp('--\\[\\[[\\s\\S]*\\]\\]--', 'gm'),		css: 'comments' },
		{ regex: new RegExp('--[^\\[]{2}.*$', 'gm'),			    css: 'comments' },	// one line comments
		{ regex: SyntaxHighlighter.regexLib.doubleQuotedString,     css: 'string' },    // strings
		{ regex: SyntaxHighlighter.regexLib.singleQuotedString,     css: 'string' },    // strings
		{ regex: new RegExp(this.getKeywords(keywords), 'gm'),		css: 'keyword' },	// keyword
		{ regex: new RegExp(this.getKeywords(funcs), 'gm'),		    css: 'func' },		// functions
		];
}

SyntaxHighlighter.brushes.Lua.prototype	= new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Lua.aliases = ['lua'];
</pre>
<p>3. 使用FTP工具登陆到WordPress空间，进入到wp-content/plugins目录，新建一个目录，取一个有意义的名字，比如syntaxhighlighter-lua；<br />
4. 将shBrushLua.js上传到新创建的目录；<br />
5. 在该目录创建一个另一个shBrushLua.php文件，添加如下内容：</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/*
Plugin Name: SyntaxHighlighter Evolved: Lua
Description: Adds support for the Lua language to the SyntaxHighlighter Evolved plugin.
Author: Benny
Version: 1.0.0
*/

// SyntaxHighlighter Evolved doesn't do anything until early in the &quot;init&quot; hook, so best to wait until after that
add_action( 'init', 'syntaxhighlighter_lua_regscript' );

// Tell SyntaxHighlighter Evolved about this new language/brush
add_filter( 'syntaxhighlighter_brushes', 'syntaxhighlighter_lua_addlang' );

// Register the brush file with WordPress
function syntaxhighlighter_lua_regscript() {
    wp_register_script( 'syntaxhighlighter-brush-lua', plugins_url( 'shBrushLua.js', __FILE__ ), array('syntaxhighlighter-core'), '1.1.1' );
}

// Filter SyntaxHighlighter Evolved's language array
function syntaxhighlighter_lua_addlang( $brushes ) {
    $brushes['lua'] = 'lua';
    return $brushes;
}
?&gt;
</pre>
<p>6. 文件都准备完了，OK，进入到WordPress后台管理的Plugins下，应该能看到新添加的一项syntaxhighlighter-lua，激活它。</p>
<p>Done! It should work now!</p>
<p>其实新添加的js和php文件也可以放到SyntaxHighlighter插件本身的目录下，但是让它独立成插件的好处是，当SyntaxHighlighter升级时，你的个人配置不会因为覆盖而丢失。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/09/add-new-brush-for-syntaxhighlighter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>基于Lua的State Pattern</title>
		<link>http://www.bennychen.cn/2011/09/lua-based-state-pattern/</link>
		<comments>http://www.bennychen.cn/2011/09/lua-based-state-pattern/#comments</comments>
		<pubDate>Sun, 04 Sep 2011 04:56:21 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[finite state machine]]></category>
		<category><![CDATA[FSM]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[state pattern]]></category>
		<category><![CDATA[状态模式]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1638</guid>
		<description><![CDATA[代码来自于最近写的Pacman，更多请查看 &#8211; https://github.com/bennychen/Moai-based-Pacman class.lua实现了在Lua中创建类的模拟，非常方便。class.lua参考自http://lua-users.org/wiki/SimpleLuaClasses State基类，包含三个stub函数，enter()和exit()分别在进入和退出state时被执行，onUpdate()函数将会在state被激活时的每帧被执行。 StateMachine类，该类集成了Moai的MOAIThread类。MOAIThread类似于Lua中的coroutine，但是在Moai中被yield的MOAIThread，会在game loop的每帧中被自动resume，见StateMachine:updateState函数，利用此特点，来实现每帧执行State:onUpdate函数。 如何利用State和StateMachine类的示例，首先定义两个state。 SampleState.lua test.lua]]></description>
			<content:encoded><![CDATA[<p>代码来自于最近写的Pacman，更多请查看 &#8211; <a href="https://github.com/bennychen/Moai-based-Pacman" target="_blank">https://github.com/bennychen/Moai-based-Pacman</a></p>
<p>class.lua实现了在Lua中创建类的模拟，非常方便。class.lua参考自<a href="http://lua-users.org/wiki/SimpleLuaClasses" target="_blank">http://lua-users.org/wiki/SimpleLuaClasses</a></p>
<pre class="brush: lua; collapse: true; light: false; title: ; toolbar: true; notranslate">
-- class.lua
-- Compatible with Lua 5.1 (not 5.0).

function class(base, init)
   local c = {}    -- a new class instance
   if not init and type(base) == 'function' then
      init = base
      base = nil
   elseif type(base) == 'table' then
    -- our new class is a shallow copy of the base class!
      for i,v in pairs(base) do
         c[i] = v
      end
      c._base = base
   end
   -- the class will be the metatable for all its objects,
   -- and they will look up their methods in it.
   c.__index = c

   -- expose a constructor which can be called by &lt;classname&gt;(&lt;args&gt;)
   local mt = {}
   mt.__call = function(class_tbl, ...)
   local obj = {}
   setmetatable(obj,c)

-- below 2 lines are updated based on the Comments from 'http://lua-users.org/wiki/SimpleLuaClasses'
--   if init then
--      init(obj,...)
   if class_tbl.init then
      class_tbl.init(obj,...)
   else
      -- make sure that any stuff from the base class is initialized!
      if base and base.init then
      base.init(obj, ...)
      end
   end
   return obj
   end
   c.init = init
   c.is_a = function(self, klass)
      local m = getmetatable(self)
      while m do
         if m == klass then return true end
         m = m._base
      end
      return false
   end
   setmetatable(c, mt)
   return c
end
</pre>
<p>State基类，包含三个stub函数，enter()和exit()分别在进入和退出state时被执行，onUpdate()函数将会在state被激活时的每帧被执行。</p>
<pre class="brush: lua; title: ; notranslate">
require &quot;class&quot;

State = class()

function State:init( name )
	self.name = name
end

function State:enter()
end

function State:onUpdate()
end

function State:exit()
end
</pre>
<p>StateMachine类，该类集成了<a href="http://getmoai.com" target="_blank">Moai</a>的MOAIThread类。MOAIThread类似于Lua中的coroutine，但是在Moai中被yield的MOAIThread，会在game loop的每帧中被自动resume，见StateMachine:updateState函数，利用此特点，来实现每帧执行State:onUpdate函数。</p>
<pre class="brush: lua; title: ; notranslate">
require &quot;State&quot;

StateMachine = class()

function StateMachine:init()
	self.currentState = nil
	self.lastState = nil
end

function StateMachine:run()
	if ( self.mainThread == nil )
	then
		self.mainThread = MOAIThread.new()
		self.mainThread:run( self.updateState, self )
	end
end

function StateMachine:stop()
	if ( self.mainThread )
	then
		self.mainThread:stop()
	end
end

function StateMachine:setCurrentState( state )
	if ( state and state:is_a( State ) )
	then
		if ( state == self.currentState )
		then
			print( &quot;WARNING @ StateMachine::setCurrentState - &quot; ..
				   &quot;var state [&quot; .. state.name .. &quot;] is the same as current state&quot; )
			return
		end
		self.lastState = self.currentState
		self.currentState = state
		if ( self.lastState )
		then
			print( &quot;exiting state [&quot; .. self.lastState.name .. &quot;]&quot; )
			self.lastState:exit()
		end
		print( &quot;entering state [&quot; .. self.currentState.name .. &quot;]&quot; )
		self.currentState:enter()
	else
		print( &quot;ERROR @ StateMachine::setCurrentState - &quot; ..
			   &quot;var [state] is not a class type of State&quot; )
	end
end

function StateMachine:updateState()
	while ( true )
	do
		if ( self.currentState ~= nil )
		then
			self.currentState:onUpdate()
		end
		coroutine.yield()
	end
end
</pre>
<p>如何利用State和StateMachine类的示例，首先定义两个state。<br />
SampleState.lua</p>
<pre class="brush: lua; title: ; notranslate">
require &quot;State&quot;

State1 = class( State ) 

function State1:init()
	State.init( self, &quot;State1&quot; )
end

function State1:enter()
	self.i = 0
end

function State1:exit()
	self.i = 0
end

function State1:onUpdate()
	print( self.name .. &quot; is updated&quot; )
	self.i = self.i + 1
	print( &quot;self.i=&quot; .. self.i )
	if ( self.i == 10 )
	then
		print( state2 )
		SM:setCurrentState( state2 )
		self.i = 0
	end
end

-----------------------

State2 = class( State ) 

function State2:init()
	State.init( self, &quot;State2&quot; )
end

function State2:onUpdate()
	print( &quot;State2 is updated&quot; )
end
</pre>
<p>test.lua</p>
<pre class="brush: lua; title: ; notranslate">
require &quot;StateMachine&quot;
require &quot;SampleState&quot;

SM = StateMachine()
SM:run()
state1 = State1()
state2 = State2()
SM:setCurrentState( state1 )
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/09/lua-based-state-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class Diagram of Pacman</title>
		<link>http://www.bennychen.cn/2011/09/class-diagram-of-pacman/</link>
		<comments>http://www.bennychen.cn/2011/09/class-diagram-of-pacman/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 16:22:55 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[class diagram]]></category>
		<category><![CDATA[Moai]]></category>
		<category><![CDATA[pacman]]></category>
		<category><![CDATA[类图]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1632</guid>
		<description><![CDATA[贴上Pacman的类图，关于这个项目，见我的上一篇博客。]]></description>
			<content:encoded><![CDATA[<p>贴上Pacman的类图，关于这个项目，见我的上一篇博客。</p>
<p><a href="http://www.bennychen.cn/wp-content/uploads/2011/09/Pacman.png" class="highslide-image" onclick="return hs.expand(this);"><img src="http://www.bennychen.cn/wp-content/uploads/2011/09/Pacman-300x267.png" alt="" title="Pacman Class Diagram" width="300" height="267" class="alignnone size-medium wp-image-1633" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/09/class-diagram-of-pacman/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>基于Moai的Pacman</title>
		<link>http://www.bennychen.cn/2011/08/moai-based-pacman/</link>
		<comments>http://www.bennychen.cn/2011/08/moai-based-pacman/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 14:49:48 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[My Projects]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[Moai]]></category>
		<category><![CDATA[oo]]></category>
		<category><![CDATA[pacman]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1621</guid>
		<description><![CDATA[最近一两个星期，在业余时间利用Moai引擎重写了当年曾经用DirectX实现过的Pacman游戏。基于Lua的代码基本上是重写，只是保留使用了之前的2D资源。重写的目的一是通过这个来熟悉Moai引擎，二是希望用一个比较好的OO结构来重新实现Pacman，三则纯粹因为好玩。 把所有的代码,资源和文档都已经上传到了github，github真的是个好东东。 https://github.com/bennychen/Moai-based-Pacman 介绍： This is a prototype game based on the Moai game platform &#8211; http://getmoai.com. With various APIs for drawing and handling different 2D elements that Moai has supported, it clones a 2D Pacman game. I programmed the game for several objectives. Firstly, because I am just new to Moai, I try to [...]]]></description>
			<content:encoded><![CDATA[<p>最近一两个星期，在业余时间利用Moai引擎重写了当年曾经用DirectX实现过的Pacman游戏。基于Lua的代码基本上是重写，只是保留使用了之前的2D资源。重写的目的一是通过这个来熟悉Moai引擎，二是希望用一个比较好的OO结构来重新实现Pacman，三则纯粹因为好玩。</p>
<p>把所有的代码,资源和文档都已经上传到了github，github真的是个好东东。</p>
<p><a href="https://github.com/bennychen/Moai-based-Pacman" target="_blank">https://github.com/bennychen/Moai-based-Pacman</a></p>
<p>介绍：</p>
<p>This is a prototype game based on the Moai game platform &#8211; <a href="http://getmoai.com" target="_blank">http://getmoai.com</a>. With various APIs for drawing and handling different 2D elements that Moai has supported, it clones a 2D Pacman game. </p>
<p>I programmed the game for several objectives. Firstly, because I am just new to Moai, I try to familiarize myself with Moai&#8217;s various APIs and game development based on it. I did encounter some issues when programming the game. Some were because I am a newbie for Moai, and some were testified later that were from Moai engine itself. Although the process was time-consuming, but to my pleasure, this is somewhat a contribution to the open source Moai engine itself. I feel a great joy to be part of it. Secondly, the whole game is based on OO. So I tried to program the whole game with a decent OO structure. Most importantly, some of the Lua classes or files, hopefully, can be reused in my other future potential games. </p>
<p>All in all, I programmed Moai-based Pacman for fun and share my code, related game assets and documents here. If there can be any help for you who are interested in Moai and OO game programming, that would be my best honer.</p>
<p><a href="http://www.bennychen.cn/wp-content/uploads/2011/08/pacman.png" class="highslide-image" onclick="return hs.expand(this);"><img src="http://www.bennychen.cn/wp-content/uploads/2011/08/pacman-262x300.png" alt="" title="pacman" width="262" height="300" class="alignnone size-medium wp-image-1626" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/08/moai-based-pacman/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>在Moai Forum的问题帖</title>
		<link>http://www.bennychen.cn/2011/08/%e5%9c%a8moai-forum%e7%9a%84%e9%97%ae%e9%a2%98%e5%b8%96/</link>
		<comments>http://www.bennychen.cn/2011/08/%e5%9c%a8moai-forum%e7%9a%84%e9%97%ae%e9%a2%98%e5%b8%96/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 02:26:15 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[forum]]></category>
		<category><![CDATA[game engine]]></category>
		<category><![CDATA[Moai]]></category>
		<category><![CDATA[MOAIAction]]></category>
		<category><![CDATA[MOAILayer2D]]></category>
		<category><![CDATA[MOAIThread]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1609</guid>
		<description><![CDATA[（关于Moai，见我的上一篇文章。） 下面贴出在Moai Forum上我最近的两个问题帖。回答者都是Patrick，他是Moai引擎的领导者，同时也是&#8217;Zipline Games&#8217;的CTO。他似乎是个不错的人，在我每次贴出问题后，总是在最快的时间内解答。 1. Does MOAILayer2D only support 512 props maximumly? http://getmoai.com/forums/moai-sdk-developer-support/does-moailayer2d-only-support-512-props-maximumly/ Me: I was trying to put lots of props onto a layer with function MOAILayer2D:insertProp, but found that if I inserted more than 512 props, all the props will fail to render on the layer. Is 512 the threshold for a layer&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>（关于Moai，见我的上一篇文章。）</p>
<p>下面贴出在Moai Forum上我最近的两个问题帖。回答者都是Patrick，他是Moai引擎的领导者，同时也是&#8217;Zipline Games&#8217;的CTO。他似乎是个不错的人，在我每次贴出问题后，总是在最快的时间内解答。</p>
<h3>1. Does MOAILayer2D only support 512 props maximumly?</h3>
<p><a href="http://getmoai.com/forums/moai-sdk-developer-support/does-moailayer2d-only-support-512-props-maximumly/" target="_blank">http://getmoai.com/forums/moai-sdk-developer-support/does-moailayer2d-only-support-512-props-maximumly/</a></p>
<p><strong>Me:</strong></p>
<p>I was trying to put lots of props onto a layer with function MOAILayer2D:insertProp, but found that if I inserted more than 512 props, all the props will fail to render on the layer. Is 512 the threshold for a layer&#8217;s prop count? Why is it a restriction? 512 doesn&#8217;t suffice my need. If there did exist a restriction, Moai even didn&#8217;t tell me any info or warning about the number 512, it took me lots of time to detect the problem and the number… <img src='http://www.bennychen.cn/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p><strong>Patrick:</strong></p>
<p>Sorry you had to spend your time on that. I agree that we could emit better warnings for situations like that.</p>
<p>The 512 restriction is arbitrary. It&#8217;s the size of the buffer created on the stack to sort the prims. I can fix it for you in the next release – will grow the buffer dynamically instead. When I have time to add an option to disable sorting by MOAILayer2D, the buffer won&#8217;t be needed at all.</p>
<p>If you need a fix immediately and are building from source, you can go into MOAILayer2D and just change it to something else.</p>
<p>(512 props is a lot! Is this for a mobile game? Mind if I ask what you&#8217;re displaying? Depending on what it is, may be other ways I can help.)</p>
<p><strong>Patrick:</strong></p>
<p>OK. I removed MAX_RENDERABLES and now use a flexible size buffer to gather and sort the props returned from the partition. That will be in the upcoming release. Try it out and let me know.</p>
<p>If you run into stuff like this in the future (any kind of odd or mysterious behavior) please let me know and I&#8217;ll hop right on it. Would much rather deal with a few false positives than have you waste your time!</p>
<p><strong>Me:</strong></p>
<p>Thanks so much for your reply and help, Patrick.</p>
<p>I&#8217;m just new to Moai. I was generating and displaying a map (31&#215;28 grids) with each grid empty or filled with a type of sprite (like a the Pacman game, each grid could be filled with a Wall or a Bean or …). So this is why I used so MANY props. Do you have some other way or suggestions for generating such kind of game maps, so that not that many props will be used?</p>
<p>As you&#8217;ve fixed this issue, so I&#8217;m looking forward to the next release. Thanks a bunch!</p>
<p><strong>Patrick:</strong></p>
<p>Absolutely. Check out MOAITileDeck2D and MOAIGrid. Any of the deck types can be used in conjunction with a grid, but MOAITileDeck2D is ideally suited. The grid draws with one transform (from the prop it&#8217;s attached to) and also gets viewport culled on a per tile basis. It&#8217;s ideally suited to tiled backgrounds. You will get way better performance and can also update the grid values at runtime or animate them using a MOAIDeckRemapper. Check out tilemap-animated for a sample. In the games we are working on in-house we have multiple layers of tilemap parallax drawing hundreds and hundred of tiles at really good frame rates. If you need to animate a tile flipping or rotating (like what happens to a block in a Mario game when Mario hits it with his head), just hide the original tile in the grid and replace it with a prop – you can use the same tile deck as the source for the prop.</p>
<p>Also, can&#8217;t say enough good things about Tiled (mapeditor.org). The latest version supports flip flags and can output its data as Lua tables you can then load in to Moai (with a little bit of work).</p>
<p>Always a pleasure to help out, so let me know if you have more questions as you go.</p>
<p><strong>Me:</strong></p>
<p>Oh, how stupid I was that I didn&#8217;t find these classes, I will try these MOAI classes later. THANK YOU!</p>
<h3>2. Questions about MOAIAction:stop</h3>
<p><a href="http://getmoai.com/forums/moai-sdk-developer-support/questions-about-moaiactionstop/">http://getmoai.com/forums/moai-sdk-developer-support/questions-about-moaiactionstop/</a></p>
<p><strong>Me:</strong></p>
<p>I have some questions regarding the usage of MOAIThread and MOAIAction</p>
<p>Firstly check out the code snippets below.</p>
<pre class="brush: jscript; title: ; notranslate">
MOAISim.openWindow ( &quot;test&quot;, 320, 480 )&lt;/span&gt;
viewport = MOAIViewport.new ()
viewport:setSize ( 320, 480 )
viewport:setScale ( 320, 480 )

layer = MOAILayer2D.new ()
layer:setViewport ( viewport )
MOAISim.pushRenderPass ( layer )

gfxQuad = MOAIGfxQuad2D.new ()
gfxQuad:setTexture ( &quot;cathead.png&quot; )
gfxQuad:setRect ( -64, -64, 64, 64 )

prop = MOAIProp2D.new ()
prop:setDeck ( gfxQuad )
layer:insertProp ( prop )

function moveThreadMain()
	movingAction = prop:moveLoc( 1000, 0, 100, MOAIEaseType.LINEAR )
	local x
	local y
	while ( movingAction:isBusy() )
	do
		x, y = prop:getLoc()
		print( &quot;current x - &quot; .. x )
		coroutine.yield()
	end
end

isCrossBound = false
function checkThreadMain()
	while ( true )
	do
		local x
		local y
		x, y = prop:getLoc()
		if ( isCrossBound == false )
		then
			if ( x &gt; 20 )
			then
				isCrossBound = true
				movingAction:stop()
				print( &quot;x after stop current frame - &quot; .. x )
			end
		else
			print( &quot;x after stop next frame - &quot; .. x )
			break
		end
		coroutine.yield()
	end
end

moveThread = MOAIThread.new()
checkThread = MOAIThread.new()
moveThread:run( moveThreadMain )
checkThread:run( checkThreadMain )
</pre>
<p>I start and run 2 threads. The &#8216;moveThread&#8217; uses MOAIAction to move the prop continuously and print its location each frame, while another &#8216;checkThread&#8217; checks if the prop crosses some bound and if yes stops the moving animation with function MOAIAction:stop</p>
<p>But the result of MOAIAction:stop differs based on the launching sequence of those above 2 threads.</p>
<p>If I run &#8216;checkThread&#8217; before &#8216;moveThread&#8217;, here is the result. MOAIAction::stop successfully stopped moving prop.</p>
<blockquote><p>x after stop current frame – 20.099973678589<br />
x after stop next frame – 20.099973678589</p></blockquote>
<p>But if I run &#8216;movingThread&#8217; before &#8216;checkThread&#8217;, I will get the following result, seems that animation will continue running for another frame</p>
<blockquote><p>x after stop current frame – 20.099973678589<br />
x after stop next frame – 20.199974060059</p></blockquote>
<p>This really confuses me. Can anyone do me a favor to explain at which condition MOAIAction will run an additional frame after stopped, and when not? Is there a way to ensure that MOAIAction:stop will run no more frame no matter the running sequence of MOAIThread?</p>
<p><strong>Patrick:</strong></p>
<p>Just ran your code. It&#8217;s a legitimate bug in the way the MOAIAction update loop works. I&#8217;ve fixed it and checked it in to &#8216;master&#8217; on moai-dev. We should have the next binary release out early next week.<br />
Thanks for finding this!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/08/%e5%9c%a8moai-forum%e7%9a%84%e9%97%ae%e9%a2%98%e5%b8%96/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moai</title>
		<link>http://www.bennychen.cn/2011/08/moai/</link>
		<comments>http://www.bennychen.cn/2011/08/moai/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 02:24:33 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[beta]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[game engine]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[Moai]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[zipline games]]></category>
		<category><![CDATA[开源]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1603</guid>
		<description><![CDATA[最近通过澳洲朋友Andrew的介绍，开始使用了一款正在开发中的开源游戏引擎Moai &#8211; the mobile platform for pro game developers. Moai是一款由&#8217;Zipline Games&#8217;公司开发的2D游戏引擎，我写这篇文章时的版本是0.5 Beta。它最大的特点就是集成了Lua脚本语言，它提供了一系列class-based的Lua API。开发者一般只需要通过写Lua脚本，即可实现一款游戏。而引擎本身解决了跨平台的问题，开发完的游戏可以顺利的发布到iOS和Android平台。而引擎本身提供的功能上，也基本上覆盖了需要开发一款2D游戏所有的元素，设备和输入，2D Sprite，动画，字体，粒子系统，物理，声音等。Moai的另外一个重要特色是提供了一个它称之为Moai Cloud的云服务，对于这个我还没有深研究，不过据称它可以让需要后端server的游戏变得简单，用户同样只需要用Lua来编写server的逻辑代码，而至于像scale-up这样的问题完全可以交给Moai引擎来处理。 然而开源引擎，尤其是处于Beta测试中的开源引擎，想用于开发正式的游戏确实有些不可靠。我最近在把之前写过的一个Pacman 2D移植到Moai上，但是遇到了很多的问题，并且一些问题最终通过层层纠结后证实是引擎本身的问题。虽然过程坎坷，但因为参与开源引擎项目，并有了自己的contribution，这本身着实是一件令人欣慰而振奋的事情。 但是希望Moai的开发者们work harder，让Moai尽快变得更好 注册并获取Moai：http://dashboard.moaicloud.com/ Moai入门文章： 第一部分：http://getmoai.com/2011/04/moai-basics-part-1/ 第二部分： http://getmoai.com/2011/04/moai-basics-part-2-2/ Moai API Documentation：http://getmoai.com/docs/]]></description>
			<content:encoded><![CDATA[<p>最近通过澳洲朋友Andrew的介绍，开始使用了一款正在开发中的开源游戏引擎<a href="http://getmoai.com" target="_blank">Moai</a> &#8211; the mobile platform for pro game developers.</p>
<p>Moai是一款由&#8217;Zipline Games&#8217;公司开发的2D游戏引擎，我写这篇文章时的版本是0.5 Beta。它最大的特点就是集成了Lua脚本语言，它提供了一系列class-based的Lua API。开发者一般只需要通过写Lua脚本，即可实现一款游戏。而引擎本身解决了跨平台的问题，开发完的游戏可以顺利的发布到iOS和Android平台。而引擎本身提供的功能上，也基本上覆盖了需要开发一款2D游戏所有的元素，设备和输入，2D Sprite，动画，字体，粒子系统，物理，声音等。Moai的另外一个重要特色是提供了一个它称之为Moai Cloud的云服务，对于这个我还没有深研究，不过据称它可以让需要后端server的游戏变得简单，用户同样只需要用Lua来编写server的逻辑代码，而至于像scale-up这样的问题完全可以交给Moai引擎来处理。</p>
<p>然而开源引擎，尤其是处于Beta测试中的开源引擎，想用于开发正式的游戏确实有些不可靠。我最近在把之前写过的一个Pacman 2D移植到Moai上，但是遇到了很多的问题，并且一些问题最终通过层层纠结后证实是引擎本身的问题。虽然过程坎坷，但因为参与开源引擎项目，并有了自己的contribution，这本身着实是一件令人欣慰而振奋的事情。</p>
<p>但是希望Moai的开发者们work harder，让Moai尽快变得更好 <img src='http://www.bennychen.cn/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>注册并获取Moai：<a href="http://dashboard.moaicloud.com/" target="_blank">http://dashboard.moaicloud.com/</a></p>
<p>Moai入门文章：<br />
第一部分：<a href="http://getmoai.com/2011/04/moai-basics-part-1/" target="_blank">http://getmoai.com/2011/04/moai-basics-part-1/</a><br />
第二部分： <a href="http://getmoai.com/2011/04/moai-basics-part-2-2/" target="_blank">http://getmoai.com/2011/04/moai-basics-part-2-2/</a></p>
<p>Moai API Documentation：<a href="http://getmoai.com/docs/ " target="_blank">http://getmoai.com/docs/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/08/moai/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Last Working Day@IBM</title>
		<link>http://www.bennychen.cn/2011/08/last-working-dayibm/</link>
		<comments>http://www.bennychen.cn/2011/08/last-working-dayibm/#comments</comments>
		<pubDate>Fri, 19 Aug 2011 15:37:47 +0000</pubDate>
		<dc:creator>Benny Chen</dc:creator>
				<category><![CDATA[A Day in the Life]]></category>
		<category><![CDATA[Something In The Way]]></category>

		<guid isPermaLink="false">http://www.bennychen.cn/?p=1599</guid>
		<description><![CDATA[一段两年的旅行今天画上了句号，这是一个终点，但又是一个全新的开始&#8230;]]></description>
			<content:encoded><![CDATA[<p>一段两年的旅行今天画上了句号，这是一个终点，但又是一个全新的开始&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennychen.cn/2011/08/last-working-dayibm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

